In a nutshell, I want to shift from running from main.py to running from some other file so that the other file can update main.py to a newer version, then reboot and run from main.py.
Alternatively, if micropython can run from memory (or in some other way) instead while updating main.py, that would be fine too.
For instance, if all that main.py did prior to executing was import from some other file and then run, then if main.py could update that other file that it just imported from, then that would work just as well, since then a reboot would lead to running updated code.
So what am I after? Ultimately, this will be so that code delivered OTA will find some way to permanently update the code micropython is running on a particular node.
I'm presently hazy as to just when it is that micropython is executing from flash memory as opposed to from RAM. For instance, when I type definitions in at the REPL prompt and then execute them, I'm assuming those definitions are in RAM, not in flash? On the other hand, if micropython is running just main.py, I'm assuming that then the code resides in flash? And if you import a module, does it stay in flash, or is it copied into RAM? If the code being executed resides in flash, then it becomes problematic to change that file out from underneath itself in the middle of execution, so to speak.
In short, if I have a file (downloaded from over the air) with an updated program in it, how can I automatically switch to having that file be main.py and then start executing it? Any suggestions?
Can MicroPython run from a file other than main.py?
Re: Can MicroPython run from a file other than main.py?
On the pyboard, in boot.py you can set the name of the main script using pyb.main(filename). If boot.py doesn't call pyb.main then it behaves as if you called pyb.main('main.py')
You can also have main.py just import another file. Once main.py is parsed it will be running the bytecode and you can change its contents and it won't affect the running version until you unload and reload it.
Your can also use __import__ to import a module using a string containing the filename.
You can also have main.py just import another file. Once main.py is parsed it will be running the bytecode and you can change its contents and it won't affect the running version until you unload and reload it.
Your can also use __import__ to import a module using a string containing the filename.
Re: Can MicroPython run from a file other than main.py?
According to the documentation:
So, it does sound as though immediately after a module is imported, the file containing the module can be completely re-written without causing any runtime problems. Assuming it's not frozen that is.When a module is imported, MicroPython compiles the code to bytecode which is then executed by the MicroPython virtual machine (VM). The bytecode is stored in RAM.
Re: Can MicroPython run from a file other than main.py?
OK, I ran an experiment, and it couldn't be easier: it appears main.py can re-write itself without issue. The changes take effect only after a re-boot.
Re: Can MicroPython run from a file other than main.py?
Yes, I do this with my own wget() function pulling master files from node js.