Executing frozen module code at REPL startup.
Posted: Tue Jun 18, 2019 4:28 pm
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 'conv-func.py'. If I create a 'boot.py' containing:
exec(open("conv-func.py").read())
Then, I can add 'boot.py' as a frozen module and in my C code I do:
pyexec_frozen_module("boot.py")
pyexec_friendly_repl()
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 'conv-func.py' as the frozen module and in my C code using pyexec_frozen_module("conv-func.py"), 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.
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 'conv-func.py'. If I create a 'boot.py' containing:
exec(open("conv-func.py").read())
Then, I can add 'boot.py' as a frozen module and in my C code I do:
pyexec_frozen_module("boot.py")
pyexec_friendly_repl()
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 'conv-func.py' as the frozen module and in my C code using pyexec_frozen_module("conv-func.py"), 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.