Can MicroPython run from a file other than main.py?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
WhiteHare
Posts: 129
Joined: Thu Oct 04, 2018 4:00 am

Can MicroPython run from a file other than main.py?

Post by WhiteHare » Tue Oct 23, 2018 9:30 pm

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?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Can MicroPython run from a file other than main.py?

Post by dhylands » Tue Oct 23, 2018 11:00 pm

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.

User avatar
WhiteHare
Posts: 129
Joined: Thu Oct 04, 2018 4:00 am

Re: Can MicroPython run from a file other than main.py?

Post by WhiteHare » Tue Oct 23, 2018 11:43 pm

According to the documentation:
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.
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.

User avatar
WhiteHare
Posts: 129
Joined: Thu Oct 04, 2018 4:00 am

Re: Can MicroPython run from a file other than main.py?

Post by WhiteHare » Wed Oct 24, 2018 12:36 am

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.

:D

bitninja
Posts: 165
Joined: Thu Sep 15, 2016 4:09 pm
Location: Spring, Texas

Re: Can MicroPython run from a file other than main.py?

Post by bitninja » Wed Oct 24, 2018 1:56 am

You are correct.

viewtopic.php?f=16&t=3827

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: Can MicroPython run from a file other than main.py?

Post by devnull » Wed Oct 24, 2018 4:39 am

Yes, I do this with my own wget() function pulling master files from node js.

Post Reply