Why is the import of *.so files from ZIP files disallowed in Python?
The documentation (https://docs.python.org/2/library/zipimport.html) is very clear:
Any files may be present in the ZIP archive, but only files .py and .py[co] are available for import. ZIP import of dynamic modules (.pyd, .so) is disallowed.
But the documentation doesn't name any reason for this strange limitation. Is is because importing from ZIP files is generally discouraged in Python? Or is it because of security reasons? If so, which ones? Is the any official statement about this?
Use of 'zipimport' module makes it possible to import Python modules and packages from ZIP-format archives. This module also allows an item of sys. path to be a string naming a ZIP file archive. Any files may be present in the ZIP archive, but only files .
To work on zip files using python, we will use an inbuilt python module called zipfile. print ( 'Done!' ) The above program extracts a zip file named “my_python_files.
To unzip a file in Python, use the ZipFile. extractall() method. The extractall() method takes a path, members, pwd as an argument and extracts all the contents. To work on zip files using Python, we will use an inbuilt python module called zipfile.
From PEP 273, Subdirectory Equivalence:
You can't satisfy dynamic modules from a zip file. Dynamic modules have extensions like
.dll
,.pyd
, and.so
. They are operating system dependent, and probably can't be loaded except from a file. It might be possible to extract the dynamic module from the zip file, write it to a plain file and load it. But that would mean creating temporary files, and dealing with all thedynload_*.c
, and that's probably not a good idea.
My interpretation is that this decision was made to avoid having the interpreter extract the dynamic module and save it to disk, as the OS's dynamic library facilities don't allow for loading from within a zip (See Windows' LoadLibraryA
and Linux's dlopen
).
While it looks like it's not technically impossible, a reason why doing the work to implement this functionality may not be worthwhile is the platform-dependence of these libraries. A .zip
containing a dynamic module that is distributed with the code that relies on it may not work in a 32-bit environment if it was created from a 64-bit environment, and wouldn't work in Linux if it was created in Windows. Disallowing dynamic modules in .zip
s may be a conscious decision to ensure that .zip
s containing Python modules will work cross-platform.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With