@pmulvey I do not see a reason for the difference, but I execute the piece of code for setting up the network interface from main.py. Actually it is in a separate file, let's call it "setup_wifi.py", and I have a statement
import setup_wifi
in main.py. It is usual practice to make main.py simple and to put you application code into separate files, which are then imported in main.py, eventually enclosed in a try/except clause. That way the device comes up in a stable state, even if you code flags errors.