How do I load precompiled python scripts?

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
cduran
Posts: 80
Joined: Thu Mar 17, 2016 4:52 pm

How do I load precompiled python scripts?

Post by cduran » Tue Aug 30, 2016 3:19 pm

I want to eliminate the mp compiler and load precompiled python scripts from memory. I just need to be pointed in the right direction.

tallyboy91
Posts: 7
Joined: Sun Aug 28, 2016 9:21 pm

Re: How do I load precompiled python scripts?

Post by tallyboy91 » Tue Aug 30, 2016 6:09 pm

I'm certainly not an expert but have gotten this to work. Basically need the micropython source code https://github.com/micropython/micropython and compile mpy-cross - https://github.com/micropython/micropyt ... /mpy-cross

You'll also need to recompile MP and flash to your device; for the WiPy I know you just need to make this change to micropython/cc3200/mpconfigport.h:

Code: Select all

#define MICROPY_PERSISTENT_CODE_LOAD (1)
After all that compile using mpy-cross and ftp to your device. Don't forget to use binary mode like I did the first few times.

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

Re: How do I load precompiled python scripts?

Post by Roberthh » Tue Aug 30, 2016 7:17 pm

Some additional bits of information to the above post. The situation differs for the various platforms:

Compiling code at build time:

For Pyboard, there is a compile option like:

Code: Select all

make FROZEN_MPY_DIR=scripts
which will compile code and pack it into flash. scripts must be a subdirectory of stmhal.
For ESP8266 python code is compiled and packed into the flash image, if it is located in esp8266/modules at build time.

Compiling code with mpy-cross and executing .mpy files from the file system:

On Pyboard, you can load mpy-cross compiled code directly.
For WPy and ESP8266, you have to modify the source code and rebuild the image as follows:

1. Add the following line to esp8266/mpconfigport.h (ES8266) or cc3200/mpconfigport.h (WiPy):

Code: Select all

#define MICROPY_PERSISTENT_CODE_LOAD (1)
2. Change line 428 of py/emitglue.c into

Code: Select all

#elif defined(__thumb2__) || defined(__xtensa__)
The line number may vary over time due to other changes in the file
and rebuild the image.

At another place there was the question discussed, whether the compiler can be omitted from the binaries. The outcome was, that this is not worth the effort, since the code space saving is minor.

cduran
Posts: 80
Joined: Thu Mar 17, 2016 4:52 pm

Re: How do I load precompiled python scripts?

Post by cduran » Tue Aug 30, 2016 7:25 pm

I'm using the minimal project. I don't have a file system (or an OS for that matter), but there is a custom loader would fetch the compiled scripts and load them into a specific flash location.

cduran
Posts: 80
Joined: Thu Mar 17, 2016 4:52 pm

Re: How do I load precompiled python scripts?

Post by cduran » Tue Aug 30, 2016 7:27 pm

Roberthh wrote:
At another place there was the question discussed, whether the compiler can be omitted from the binaries. The outcome was, that this is not worth the effort, since the code space saving is minor.
I see, if that's the case I wont bother omitting the compiler, but I still would like to transfer the scripts as compiled binaries. Sending them as strings is not very efficient.

Post Reply