8000 unix: can't import frozen modules from a script that was executed from a file · Issue #2322 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content
8000

unix: can't import frozen modules from a script that was executed from a file #2322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dpgeorge opened this issue Aug 15, 2016 · 13 comments
Closed

Comments

@dpgeorge
Copy link
Member

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:

  1. Add a hack to the import machinery to always search frozen modules.
  2. Add a special directory to sys.path (eg :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.
@dpgeorge dpgeorge added the bug label Aug 15, 2016
@pfalcon
Copy link
Contributor
pfalcon commented Aug 15, 2016

I don't think I like # 2. Too much adhoc magic, which likely will interfere with resource (non-executable) file access.

@dpgeorge
Copy link
Member Author

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.

@pfalcon
Copy link
Contributor
pfalcon commented Feb 6, 2017

I'm now being hit by this and eager to fix this ;-).

@pfalcon
Copy link
Contributor
pfalcon commented Feb 6, 2017

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.

@pfalcon
Copy link
Contributor
pfalcon commented Feb 6, 2017

The current workaround is to specify empty path component via MICROPYPATH (de48a27 fixes a case when there're multiple path components, first empty).

@dpgeorge
Copy link
Member Author
dpgeorge commented Feb 7, 2017

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.

@pfalcon
Copy link
Contributor
pfalcon commented Apr 30, 2017

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).

@pfalcon
Copy link
Contributor
pfalcon commented Apr 30, 2017

Ok, now frozen mpy's don't seem to work even from repl.

@pfalcon
Copy link
Contributor
pfalcon commented Apr 30, 2017

Ok, that's because frozen mpy is disable for unix port.

8000

@pfalcon
Copy link
Contributor
pfalcon commented Apr 30, 2017

...or not

@pfalcon
Copy link
Contributor
pfalcon commented Apr 30, 2017

Ok, something just glitches in make apparently.

@belyalov
Copy link

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'

wnienhaus pushed a commit to wnienhaus/micropython that referenced this issue Sep 5, 2021
This change "moves" frozen modules into a virtual directory
called ".frozen". This allows controlling the search order for
frozen modules, relative to other directories.

Before this change, frozen modules were found via the current
directory (i.e. the "" entry in sys.path). However, when a file
is executed from the command line the first entry in sys.path
(which is normally the empty string ""), is replaced by the base
path of the executing file (correct behaviour as per CPython).
This made loading frozen modules fail.

With this change, frozen modules have their own entry in
sys.path, which also allows the user to control the order of
search when looking for frozen modules.

By default, the virtual ".frozen" directory is the last entry
in sys.path (i.e. it gets searched last).

Fixes issue micropython#2322.
andrewleech pushed a commit to andrewleech/micropython that referenced this issue Nov 17, 2021
This change "moves" frozen modules into a virtual directory
called "|frozen". This allows controlling the search order for
frozen modules, relative to other directories.

Before this change, frozen modules were found via the current
directory (i.e. the "" entry in sys.path). However, when a file
is executed from the command line the first entry in sys.path
(which is normally the empty string ""), is replaced by the base
path of the executing file (correct behavior as per CPython).
This made loading frozen modules fail.

With this change, frozen modules have their own entry in
sys.path, which also allows the user to control the order of
search when looking for frozen modules.

By default, the virtual "|frozen" directory is the last entry
in sys.path (i.e. it gets searched last).

Fixes issue micropython#2322.
andrewleech pushed a commit to andrewleech/micropython that referenced this issue Nov 18, 2021
…xisting entry.

Also applies to windows port.

Addresses issue micropython#2322
andrewleech pushed a commit to andrewleech/micropython that referenced this issue Nov 18, 2021
This allows any frozen code to still be found with existing empty entry.
Also applies to windows port.

Addresses issue micropython#2322
andrewleech pushed a commit to andrewleech/micropython that referenced this issue Nov 25, 2021
This allows any frozen code to still be found with existing empty entry.
Also applies to windows port.

Addresses issue micropython#2322
dpgeorge pushed a commit to dpgeorge/micropython that referenced this issue Dec 17, 2021
This changes makemanifest.py & mpy-tool.py to merge string and mpy names
into the same list (now mp_frozen_names).

The various paths for loading a frozen module (mp_find_frozen_module) and
checking existence of a frozen module (mp_frozen_stat) use a common
function that searches this list.

In addition, the frozen lookup will now only take place if the path starts
with ".frozen", which needs to be added to sys.path.

This fixes issues micropython#1804, micropython#2322, micropython#3509, micropython#6419.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
@dpgeorge
Copy link
Member Author

Fixed by e0bf461

andrewleech pushed a commit to andrewleech/micropython that referenced this issue Jan 24, 2022
This allows any frozen code to still be found with existing empty entry.
Also applies to windows port.

Addresses issue micropython#2322
leifbirger pushed a commit to leifbirger/micropython that referenced this issue Jun 14, 2023
This changes makemanifest.py & mpy-tool.py to merge string and mpy names
into the same list (now mp_frozen_names).

The various paths for loading a frozen module (mp_find_frozen_module) and
checking existence of a frozen module (mp_frozen_stat) use a common
function that searches this list.

In addition, the frozen lookup will now only take place if the path starts
with ".frozen", which needs to be added to sys.path.

This fixes issues micropython#1804, micropython#2322, micropython#3509, micropython#6419.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants
0