[SOLVED] Version 1.18 cannot import frozen modules, ImportError

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
BetterAutomations
Posts: 83
Joined: Mon Mar 20, 2017 10:22 pm

[SOLVED] Version 1.18 cannot import frozen modules, ImportError

Post by BetterAutomations » Tue Jan 25, 2022 7:09 pm

What do I need to do to import frozen modules with 1.18? This worked fine under 1.16.

Code: Select all

>>> import _main
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: no module named '_main'
>>> 
Last edited by BetterAutomations on Tue Jan 25, 2022 9:05 pm, edited 1 time in total.

BetterAutomations
Posts: 83
Joined: Mon Mar 20, 2017 10:22 pm

Re: Version 1.18 cannot import frozen modules, ImportError

Post by BetterAutomations » Tue Jan 25, 2022 8:45 pm

I see something in the release notes about frozen modules but I don't understand how it impacts me, or how to work with this.
Support for FROZEN_DIR and FROZEN_MPY_DIR has been deprecated for some time and was finally removed in this release. Instead of these, FROZEN_MANIFEST can be used. The io.resource_stream() function is also removed, replaced by the pure Python version in micropython-lib.

The search order for importing frozen Python modules is now controlled by the ".frozen" entry in sys.path. This string is added by default in the second position in sys.path. User code should adjust sys.path depending on the desired behaviour. Putting ".frozen" first in sys.path will speed up importing frozen modules.

Code: Select all

>>> import sys
>>> sys.path
['/lib', '/fresh', '']
>>> 

BetterAutomations
Posts: 83
Joined: Mon Mar 20, 2017 10:22 pm

Re: [SOLVED] Version 1.18 cannot import frozen modules, ImportError

Post by BetterAutomations » Tue Jan 25, 2022 9:06 pm

Solved. I was adding a custom path to sys.path and removing all other paths and accidentally removed .frozen as well. Now that I leave it alone, my sys.path is correct:

Code: Select all

>>> import sys
>>> sys.path
['', '.frozen', '/lib']
>>> import _main
>>> 

Post Reply