On this page
zipapp
Prev: xmlrpc | Up: stdlib_module | Next: zipfile
Rel: sys, sysconfig, builtins, warnings, dataclasses
Ref: docs.python.org — zipapp
Tags: #public #python3 #stdlib
Import
import zipapp
Overview
Packages a pure-Python directory into a single executable .pyz zip archive (create_archive, python -m zipapp) that CPython runs directly (python app.pyz or ./app.pyz with a shebang). Ideal for distributing zero-dependency CLI scripts as one portable file.
Quick Start
# Bundle ./myapp (containing myapp/__main__.py or main:run) into one executable archive:
python3 -m zipapp myapp -m "myapp:main" -p "/usr/bin/env python3" -o myapp.pyz
python3 myapp.pyz
Implementation Walkthrough
1. Building a .pyz Archive Programmatically (zipapp.create_archive)
from pathlib import Path
import tempfile
import zipapp
with tempfile.TemporaryDirectory() as tmp:
src = Path(tmp) / "src"
src.mkdir()
(src / "__main__.py").write_text("print('bundled ok')\n", encoding="utf-8")
out = Path(tmp) / "demo.pyz"
zipapp.create_archive(src, target=out, interpreter="/usr/bin/env python3", compressed=True)
assert out.exists() and zipapp.get_interpreter(out) == "/usr/bin/env python3"
Gotchas & Boundaries
- Pure-Python modules only: C extensions (
.so/.pyd) cannot be imported directly from inside a zip archive. - Don't rely on
__file__paths inside a.pyz: Read packaged data files viaimportlib.resources.files(...)instead. - Pass
compressed=Trueto shrink the resulting.pyz(default is uncompressedZIP_STORED).
Common Functions
- zipapp.create_archive() —
create_archive(source, target=None, interpreter=None, main=None, filter=None, compressed=False)— Create an application archive from SOURCE. - zipapp.get_interpreter() —
get_interpreter(archive)—zipapp.get_interpreter()function.
Classes & Objects
- zipapp.ZipAppError —
zipapp.ZipAppErrorclass. (Methods:.add_note(),.with_traceback())
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/xmlrpc
- tuts/python3/stdlib_module/zipapp/create_archive
- tuts/python3/stdlib_module/zipapp/get_interpreter
- tuts/python3/stdlib_module/zipapp/ZipAppError
- tuts/python3/stdlib_module/zipfile
- tuts/python3/stdlib_module/zipimport
- tuts/python3/tutorials/intermediate_python_and_software_engineering/modules_packages_and_tooling
Related notes
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/xmlrpc Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/zipapp/create_archive Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/zipapp/get_interpreter Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/zipapp/ZipAppError 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 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