Python zipimport Module
Example
Create a ZIP file with a module and import from it:
import zipfile
import zipimport
# Create a ZIP with a simple module
with zipfile.ZipFile('mymodules.zip', 'w') as z:
z.writestr('greet.py', 'def hello(name): return f"Hello, {name}!"')
# Import from ZIP using zipimport
importer = zipimport.zipimporter('mymodules.zip')
greet = importer.load_module('greet')
print(greet.hello('Linus'))
Try it Yourself »
Definition and Usage
The zipimport module allows Python to import modules directly from ZIP archives without extracting them first.
This is useful for distributing Python packages as single ZIP files or for running code from compressed archives.
Note: Python automatically supports ZIP imports when a .zip file is added to sys.path.
Members
Member | Description |
---|---|
zipimporter | Importer class that can import Python modules from ZIP archives. |
ZipImportError | Exception raised by zipimporter when import fails from a ZIP archive. |