On this page
zipimport
Prev: zipfile | Up: stdlib_module | Next: zlib
Rel: sys, sysconfig, builtins, warnings, dataclasses
Ref: docs.python.org — zipimport
Tags: #public #python3 #stdlib
Import
import zipimport
Overview
Import hook (zipimporter) enabling Python to import .py and .pyc modules directly from .zip archives on sys.path (the mechanism behind zipapp .pyz bundles and wheel/egg imports). Reach for zipimport when loading plugins or self-contained Python bundles directly from a zip archive without extracting them to disk.
Quick Start
from pathlib import Path
import tempfile
import zipfile
import zipimport
with tempfile.TemporaryDirectory() as tmp:
archive = Path(tmp) / "bundle.zip"
with zipfile.ZipFile(archive, "w") as zf:
zf.writestr("greeter.py", "def hi(name): return f'hello {name}'\n")
importer = zipimport.zipimporter(str(archive))
mod = importer.load_module("greeter")
assert mod.hi("world") == "hello world"
Implementation Walkthrough
1. Reading Data Files Bundled Inside a Zip Archive
zipimporter.get_data(pathname) reads non-Python assets stored alongside modules inside the archive:
from pathlib import Path
import tempfile
import zipfile
import zipimport
with tempfile.TemporaryDirectory() as tmp:
archive = Path(tmp) / "assets.zip"
with zipfile.ZipFile(archive, "w") as zf:
zf.writestr("pkg/__init__.py", "")
zf.writestr("pkg/data.txt", "payload")
importer = zipimport.zipimporter(str(archive))
raw = importer.get_data(str(archive / "pkg" / "data.txt"))
assert raw == b"payload"
Gotchas & Boundaries
- Pure Python only:
zipimportcannot load C extension modules (.so/.pyd) directly from inside a zip archive—native extensions must exist on the filesystem. - Prefer
sys.path+importlibfor normal imports: Appending a.zippath tosys.pathactivateszipimportautomatically viasys.path_hooks, andimportlib.resourcesis the modern way to read package data. - Cached zip directory: If a zip archive is modified on disk after creation, call
importer.invalidate_caches()sozipimportreloads its central directory.
Classes & Objects
- zipimport.zipimporter — Create a new zipimporter instance. 'archivepath' must be a path to a zipfile, or to a specific path inside a zipfile. For example, it can be '/tmp/myimport.zip', or '/tmp/myimport.zip/mydirectory', if mydirectory is a (Methods:
.create_module(),.exec_module(),.find_spec(),.get_code(),.get_data(),.get_filename()) - zipimport.ZipImportError —
zipimport.ZipImportErrorclass. (Methods:.add_note(),.with_traceback())
Linked from
Related notes
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/zipfile Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/zipimport/zipimporter Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/zipimport/ZipImportError Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/zlib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/builtins Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/dataclasses Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/sys Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/sysconfig Linked here; shared tags: #stdlib