ImportError with Frozen Modules

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
ProudPagan
Posts: 35
Joined: Fri Apr 12, 2019 8:55 am

ImportError with Frozen Modules

Post by ProudPagan » Tue Apr 30, 2019 3:16 pm

Hi,

I am trying to freeze an MP module as described here: https://docs.micropython.org/en/latest/ ... ght=frozen

When I load the module from the MP prompt, the module is loaded correctly, but when I try to execute a file main.py that
imports the frozen module, I see:

Code: Select all

# ./micropython main.py
Traceback (most recent call last):
  File "main.py", line 1, in <module>
ImportError: no module named 'iceCream'
main.py does nothing except:

Code: Select all

import iceCream
Is there anything I'm missing?

Thanks for any help!

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: ImportError with Frozen Modules

Post by dhylands » Wed May 01, 2019 12:26 am

This seems to be a quirk? of the unix version of micropython. I was able to reproduce this by creating a file iceCream.py that contained:

Code: Select all

print('Executing iceCream.py')
and copying that into the modules directory and rebuilding micropython. As you did, I get:

Code: Select all

637 >./micropython 
MicroPython v1.10-290-g8402c26cf-dirty on 2019-04-30; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import iceCream
Executing iceCream.py
>>> 
and with a main.py that contained:

Code: Select all

import iceCream
then I get:

Code: Select all

637 >./micropython main.py
Traceback (most recent call last):
  File "main.py", line 4, in <module>
ImportError: no module named 'iceCream'
The next thng I did was to print sys.path for each of these cases:

Code: Select all

638 >./micropython 
MicroPython v1.10-290-g8402c26cf-dirty on 2019-04-30; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import sys
>>> print(sys.path)
['', '/home/dhylands/.micropython/lib', '/usr/lib/micropython']
>>> 
and

Code: Select all

640 >./micropython main.py
['/home/dhylands/Dropbox/micropython/micropython/ports/unix', '/home/dhylands/.micropython/lib', '/usr/lib/micropython']
Traceback (most recent call last):
  File "main.py", line 4, in <module>
ImportError: no module named 'iceCream'
and this shows us the difference. The empty string in sys.path is where the interpreter will look for frozen modules, and when executing main.py there is no empty path added to sys.path. If we add it ourselves:

Code: Select all

import sys
sys.path.insert(0, '')
print(sys.path)
import iceCream
then everything works as expected:

Code: Select all

646 >./micropython main.py
['', '/home/dhylands/Dropbox/micropython/micropython/ports/unix', '/home/dhylands/.micropython/lib', '/usr/lib/micropython']
Executing iceCream.py

ProudPagan
Posts: 35
Joined: Fri Apr 12, 2019 8:55 am

Re: ImportError with Frozen Modules

Post by ProudPagan » Wed May 01, 2019 5:55 am

Perfect. Your sys.path explanation makes sense, because that's how "Mega" Python works
too, but I would have expected the /null/ path to be appended to sys.path by default in
MicroPython.

Thanks very much!

Post Reply