-
-
Notifications
You must be signed in to change notification settings - Fork 30.5k
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
zipimport cannot do a namespace import when a directory has no python files, but it contains nested directories with python files inside of a zip file. #121111
Comments
The issue was originally identified in this issue as well: SeaHOH/memimport#3 |
(I'd like to work on this issue, please assign it to me.) A quick repro:
import zipimport
import importlib.util
x = zipimport.zipimporter("x.zip")
spec = x.find_spec("x")
mod = importlib.util.module_from_spec(spec)
x.exec_module(mod) # Error! (As an additional note, typeshed does not have the Although, this works just fine when you do it the normal way: import sys
sys.path.append("./x.zip")
import x |
Ah, FWIW, I was being dumb - my example here is unable to import a submodule anyway: import sys
sys.path.append("./x.zip")
import x (I don't think this is something that could be added, and out of the scope of this issue anyway.) Here's the actual bug: Using the following commands as setup:
The following works using import sys
sys.path.append("a.zip")
import a.b.c but the following does not, using import zipimport
importer = zipimport.zipimporter("./a.zip")
importer.get_code("a")
importer.get_code("a.b") # Error! |
it is odd that |
I didn't test that - does that fail? (Note that you need an |
Here is how to repro: #!/usr/bin/env python3
# test.py
import zipfile
with zipfile.ZipFile('a.zip', 'w') as zf:
zf.writestr('a/__init__.py', b'')
zf.writestr('a/b/c/__init__.py', b'')
import sys
sys.path.append("a.zip")
import a.b.c
print(a.b) $ ./test.py
Traceback (most recent call last):
File "./test.py", line 12, in <module>
import a.b.c
ModuleNotFoundError: No module named 'a.b' When add this line in create zip file: zf.mkdir('a/b/') # need py311 $ ./test.py
<module 'a.b' (<_frozen_importlib_external.NamespaceLoader object at 0x0000000000D371D0>)> |
It looks rather like a new feature, so no. There is a simple workaround -- always add explicit directory entries to the ZIP file. |
It seems that the original issue has been resolved. |
Just a quick comment to say that the
Did I miss anything? |
These tests are passed on my computer and on all buildbots. It seems that something is wrong with your setup. Did you forget to rebuild Python? |
I blew the build directory and the error went away. Apologies for the confusion. |
Bug report
Bug description:
When on the filesystem packages like discord.py (
pip install discord.py
) works just fine (from discord.ext.commands import Bot
).However, when you use the same code used to create the python312.zip file to put that package inside of a zip file (the aiohttp dependency would need to be loaded from the user site-packages folder as it contains c extensions files inside of it's package) then
zipimport
's_is_dir
bugs out and says that it cannot import modulediscord.ext
because it most likely thinks it is a file (it is not as it clearly contains nested directories).The fix:
The simple fix is if the first check returns
false
, is to do a recursive subdirectory check and see if it has subdirectories of any nesting levels with python files inside (__init__.py[c]
, etc) and to returntrue
if that is the case.Temporary solution:
Manually create an empty
__init__.py
when the interpreter sees this sort of issue and contribute it back to the package owner.While it might sound like a good suggestion at first, some maintainers like to randomly block people from their github from filing issues that they identify as a
skill issue
so the best fix is the one inzipimport
as there is no control over what package authors do, but there are use cases for zipping everything into a single zip file as well (embedding). Also fixingzipimport
would be a simple patch once and done chore as well as opposed to filing an issue with every package that a person could place into a zip file and use.CPython versions tested on:
3.12, 3.13, CPython main branch
Operating systems tested on:
Linux, macOS, Windows
Linked PRs
zipimport
incorrectly dealing with namespace packages #121161The text was updated successfully, but these errors were encountered: