Repository navigation
unix: can't import frozen modules from a script that was executed from a file #2322
Description
Activity
I don't think I like # 2. Too much adhoc magic, which likely will interfere with resource (non-executable) file access.
Note that frozen bytecode (frozen mpy files) can't really be exposed as a read-only file (unlike frozen strings), so they'd anyway need to live in a separate "virtual" filesystem to any frozen resource files.
I'm now being hit by this and eager to fix this ;-).
Add a hack to the import machinery to always search frozen modules.
So, I can see following way: if sys.path[0] != "", search before it only frozen modules. That at least should give the same search order as for REPL. But what did we decide on overridability of frozen modules? Actually, need to check what CPython does wrt to frozen modules search order and overridability.
The current workaround is to specify empty path component via MICROPYPATH (de48a27 fixes a case when there're multiple path components, first empty).
But what did we decide on overridability of frozen modules?
I think it's useful and important to be able to control the order (and whether they are even searched) of import of frozen modules. Controlling that via sys.path seems the most obvious way.
It now seems that behavior is different from str vs mpy frozen modules (which is probably rooted in the fact that we have inconsistent, duplicated code to look up each type).
Ok, now frozen mpy's don't seem to work even from repl.
Ok, that's because frozen mpy is disable for unix port.
...or not
Ok, something just glitches in make apparently.
Any updates on this?
It looks to be still broken:Having a build of micropython with unittests module frozen:
root@e1ec92421b56:/build# micropython MicroPython v1.9.3-477-g7b0a020 on 2018-03-21; linux version Use Ctrl-D to exit, Ctrl-E for paste mode >>> import unittest >>>
Simple script
root@e1ec92421b56:/build# cat z.py import unittest
Doesn't work:
root@e1ec92421b56:/build# micropython z.py Traceback (most recent call last): File "z.py", line 1, in <module> ImportError: no module named 'unittest'
- added a commit that references this issue
on Sep 5, 2021 - added 3 commits that reference this issue
on Nov 17, 2021 - added a commit that references this issue
on Nov 25, 2021 - added a commit that references this issue
on Dec 17, 2021 Fixed by e0bf461
- added a commit that references this issue
on Jan 24, 2022 - added a commit that references this issue
on Jun 14, 2023 - added a commit that references this issue
on May 23, 2026 - added a commit that references this issue
on Sep 21, 2026
The unix port can import frozen modules (str and mpy) from the REPL but it can't import them when executing a file (eg from
./micropython test.py). This is because when a file is executed from the command line the first entry in sys.path (which is originally the empty string '') is replaced by the base path of the executing file. This is the correct (CPython) behaviour.But it means that frozen modules can no longer be found in the path -- they require the empty string in the path in order for them to be found in the import search (note that bare-metal ports always have '' in the path so they are ok).
Some ways to fix:
:frozen:/) and prefix all the filenames in the list of frozen files with this special directory. The benefit of this is that it would allow the user to control the order of search when looking for frozen modules.