Frozen modules and search order
Frozen modules and search order
Do i understand correctly frozen modules are always the first in the search order when doing an import?
For example let's assume I have a frozen module called foo. I made some changes to it and would like to test the changes so I upload foo.py to the flash. However when I do import foo it always seems to import the frozen module.
Do I have any other options than flashing a new firmware without the frozen module?
For example let's assume I have a frozen module called foo. I made some changes to it and would like to test the changes so I upload foo.py to the flash. However when I do import foo it always seems to import the frozen module.
Do I have any other options than flashing a new firmware without the frozen module?
Re: Frozen modules and search order
I wonder if you could import a randomname module, then system.modules['modulename'] = randomname before importing anything else at startup.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Frozen modules and search order
Frozen modules are found first. This has caught me out more than once
@cefn I'm afraid I don't follow your solution, perhaps you can clarify? The value of the sys.modules dict is a module instance rather than a module name. But perhaps I'm missing your point here.
@cefn I'm afraid I don't follow your solution, perhaps you can clarify? The value of the sys.modules dict is a module instance rather than a module name. But perhaps I'm missing your point here.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Frozen modules and search order
Sorry for the last post. Was rushing the post in on a phone, autocorrect messed things up and wasn't able to put together a proper code example. I was referring to the strategy shared by @stijn in viewtopic.php?f=2&t=4449&p=25753&hilit=monkey#p25753
After putting a file myfrozenmodule123123.mpy in the filesystem, you might do...
However, I don't now if the strategy might not work with frozen modules for some reason.
After putting a file myfrozenmodule123123.mpy in the filesystem, you might do...
Code: Select all
# make sure this happens before anything else
import myfrozenmodule123123
sys.modules['myfrozenmodule']=myfrozenmodule123123
# start loading modules which import myfrozenmodule now, and they should end up receiving the substitute module
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Frozen modules and search order
A quick test suggests that this does work. A useful technique.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Frozen modules and search order
If you examine sys.path, then you'll see that there is often an empty string shown in the array. When micropython encounters this empty string, this is where it looks for frozen modules. By default, sys.path looks like this:tuupola wrote: ↑Fri Mar 02, 2018 10:48 amDo i understand correctly frozen modules are always the first in the search order when doing an import?
For example let's assume I have a frozen module called foo. I made some changes to it and would like to test the changes so I upload foo.py to the flash. However when I do import foo it always seems to import the frozen module.
Do I have any other options than flashing a new firmware without the frozen module?
Code: Select all
>>> import sys
>>> sys.path
['', '/flash', '/flash/lib']
Code: Select all
>>> del sys.path[0]
>>> sys.path.append('')
>>> sys.path
['/flash', '/flash/lib', '']
I was able to confirm this on my pyboard. By default there is an lcd160cr.py module which is frozen. I created a /flash/lcd160cr.py file which contained this:
Code: Select all
print('Custom lcd160cr.py loaded')
Code: Select all
>>> import lcd160cr
>>>
Code: Select all
>>> import sys
>>> sys.path
['', '/flash', '/flash/lib']
>>> del sys.path[0]
>>> sys.path.append('')
>>> sys.path
['/flash', '/flash/lib', '']
>>> import lcd160cr
Custom lcd160cr.py loaded
>>>
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact: