right way to use zipimport, zipimport.ZipImportError: not a Zip file

dieter dieter at handshake.de
Wed Aug 15 02:08:31 EDT 2018


iMath <redstone-cold at 163.com> writes:
> ...

> I think someone gives the true reason caused the exception here
> https://stackoverflow.com/a/51821910/1485853
>
> Thanks to his  explanation , I extracted the zip archive and then add the extracted to a zip archive using Bandizip, this time `zipimport.zipimporter(r'C:\Users\i\Downloads\you-get-0.4.1128.zip') ` doesn't give the exception , but still cannot import the module, even adding the  `.zip`  file to  `sys.path`,
>
>     >>> import sys
>     >>> sys.path.insert(0, r'C:\Users\i\Downloads\you-get-0.4.1128.zip\you-get-0.4.1128\src')
>     >>> from you_get import common
>     Traceback (most recent call last):
>       File "<pyshell#17>", line 1, in <module>
>         from you_get import common
>     ModuleNotFoundError: No module named 'you_get'
>     >>> 
>
>
>     >>> import zipimport
>     >>> z=zipimport.zipimporter(r'C:\Users\i\Downloads\you-get-0.4.1128.zip\you-get-0.4.1128\src')
>     >>> z
>     <zipimporter object "C:\Users\i\Downloads\you-get-0.4.1128.zip\you-get-0.4.1128\src\">
>     >>> z.load_module('you_get.common')
>     Traceback (most recent call last):
>       File "<pyshell#9>", line 1, in <module>
>         z.load_module('you_get.common')
>     zipimport.ZipImportError: can't find module 'you_get.common'
>     >>> z.load_module('you_get')
>     Traceback (most recent call last):
>       File "<pyshell#10>", line 1, in <module>
>         z.load_module('you_get')
>     zipimport.ZipImportError: can't find module 'you_get'

I tried (on Linux):
lsdm: python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sys import path; path.insert(0, "you-get-0.4.1128.zip/you-get-0.4.1128/src")
>>> from you_get import common
>>> common.__file__
'you-get-0.4.1128.zip/you-get-0.4.1128/src/you_get/common.py'
>>> 

This means that Python can import correctly from the (repackaged) zip archive.

Try to use "/" in place of "\" in the path specification.
The replacement may be necessary for the subpath inside the zip archive
and Python supports "/" as filepath separator even on Windows.




More information about the Python-list mailing list