Page 1 of 1

Executing frozen module code at REPL startup.

Posted: Tue Jun 18, 2019 4:28 pm
by brianh
Hi. I am new to MicroPython and I've been experimenting with building and running (REPL) on an ARMv6. I'm sure I am misunderstanding some basic concepts re: frozen modules. What I want to do is have a simple uPy boot script that is frozen and which executes before I enter the REPL. The primary goal is just to pre-define some convenience functions in uPy that can be used from the REPL and without even having to type 'import blah...' to enable them.

The problem is that if I freeze the uPy function definitions directly, it doesn't work at all. None of the functions are available at the REPL prompt. However, somewhat strange to me, is that I can freeze a module that executes a module on the disk and it works fine.

So, assume my functions are defined in ''. If I create a '' containing:

Then, I can add '' as a frozen module and in my C code I do:
This works fine, and my convenience functions are immediately available in the REPL, having beed defined by executing the script from disk.

BUT, if I try just adding '' as the frozen module and in my C code using pyexec_frozen_module(""), then it doesn't seem to work and the functions are undefined until I 'import conv-func'. Is there a reason that it works this way?

I'm glad I have this relatively simple workaround, but it seems to unnecessarily complicate the process. I'm also not sure it can be solved by freezing both modules and using one to import the other. I might be stuck with having the main functionality needing to be loaded from disk, which is definitely problematic.

Any advice to simplify, or just fix my flawed understanding of frozen module, would be appreciated.

Re: Executing frozen module code at REPL startup.

Posted: Wed Jun 19, 2019 12:48 am
by jimmo
I'm not 100% sure this is relevant, but which type of frozen modules are you using? (i.e. are you defining FROZEN_DIR or FROZEN_MPY_DIR).

Re: Executing frozen module code at REPL startup.

Posted: Wed Jun 19, 2019 1:26 am
by dhylands
On my pyboard I put the following file into the modules directory:

Code: Select all

print('Executing frozen')

def myfunc():
    print('Executing myfunc')
When I boot the board, I see this:

Code: Select all

Executing frozen
MicroPython v1.11-47-g1a51fc9dd on 2019-06-18; PYBv1.1 with STM32F405RG
Type "help()" for more information.
and then I can execute myfunc without having to import anything:

Code: Select all

>>> myfunc()
Executing myfunc

Re: Executing frozen module code at REPL startup.

Posted: Wed Jun 19, 2019 3:22 pm
by brianh
Yep, that's exactly what I want. Well now that I know it is suppose to work, I need to look more deeply into my build scripts to see where the code is (probably) misconfigured. I don't have a pyboard, but I'll try to reproduce your results in ports/unix, then use that as a basis to compare to my ARM build, which I derived starting from ports/minimal. But I've made numerous changes to the configuration in mpconfigport.h, and probably broken frozen module somehow.

I'll report back my findings.

Re: [SOLVED] Executing frozen module code at REPL startup.

Posted: Wed Jun 19, 2019 4:01 pm
by brianh
Thanks for the feedback, gentleman. Lo and behold, it is working fine for me now.

I didn't change anything and the frozen module is now running fine at startup and defines my convenience functions as desired, without resorting to the exec() workaround. The only thing I can figure is that I had messed up my frozen module during experimentation with the "-X emit=native" switch of the 'mpy-cross' tool. A fresh build of the module, not using -X, and it works fine.