On this page
  1. zipapp
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Building a .pyz Archive Programmatically (zipapp.create_archive)
  7. Gotchas & Boundaries
  8. Common Functions
  9. Classes & Objects

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

Common Functions

Classes & Objects

Linked from

Related notes