Frozen bytecode main.py

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
ajie_dirgantara
Posts: 81
Joined: Fri Sep 02, 2016 9:26 am

Frozen bytecode main.py

Post by ajie_dirgantara » Wed Aug 15, 2018 6:47 am

I am on production phase and would like to froze main.py and other modules into the micropython binary file, so after it burned into flash it will run directly. is this possible?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Frozen bytecode main.py

Post by Roberthh » Wed Aug 15, 2018 7:15 am

The resepctive code is around https://github.com/micropython/micropyt ... main.c#L68
There you see, that a file called _boot.py is run from flash, and that boot.py and main.py are run form the file system. You can either change the call for boot.py and main.py into pyexec_frozen_module(), or put the start of your modules into _boot.py.

ajie_dirgantara
Posts: 81
Joined: Fri Sep 02, 2016 9:26 am

Re: Frozen bytecode main.py

Post by ajie_dirgantara » Thu Aug 16, 2018 8:00 am

Roberthh wrote:
Wed Aug 15, 2018 7:15 am
The resepctive code is around https://github.com/micropython/micropyt ... main.c#L68
There you see, that a file called _boot.py is run from flash, and that boot.py and main.py are run form the file system. You can either change the call for boot.py and main.py into pyexec_frozen_module(), or put the start of your modules into _boot.py.
Ok, so in short, after adding my main.py in "modules" directory, and then by modify the _boot.py by add "import main.py" line at below is enough?

Code: Select all

import gc
gc.threshold((gc.mem_free() + gc.mem_alloc()) // 4)
import uos
from flashbdev import bdev

try:
    if bdev:
        uos.mount(bdev, '/')
except OSError:
    import inisetup
    inisetup.setup()

gc.collect()

import main.py

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Frozen bytecode main.py

Post by Roberthh » Thu Aug 16, 2018 8:15 am

That should work. I only would not call that file main.py, to avoid conflicts. You can give is any other name. In addition you can have a main.py in your file system, which then might be empty. abd obviously, if your file is called "myfile.py", the statement should read:
import myfile

ajie_dirgantara
Posts: 81
Joined: Fri Sep 02, 2016 9:26 am

Re: Frozen bytecode main.py

Post by ajie_dirgantara » Thu Aug 16, 2018 8:34 am

ok, this is a bit out of topic, but can we also froze "webrepl_cfg.py", so it doesn't need to be configured one by one?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Frozen bytecode main.py

Post by Roberthh » Thu Aug 16, 2018 8:59 am

you can freeze any python script, which is about to be imported. And you can simply try it.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Frozen bytecode main.py

Post by pythoncoder » Sat Aug 18, 2018 8:22 am

The approach I have used is to have _boot.py create main.py if it doesn't exist. The new main.py imports my module (mqtt).

_boot.py:

Code: Select all

import gc
gc.threshold((gc.mem_free() + gc.mem_alloc()) // 4)
import uos
from flashbdev import bdev

try:
    if bdev:
        uos.mount(bdev, '/')
except OSError:
    import inisetup
    inisetup.setup()

try:
    uos.stat('/main.py')
except OSError:
    with open("/main.py", "w") as f:
        f.write("""\
import mqtt
""")

gc.collect()
Peter Hinch
Index to my micropython libraries.

Post Reply