brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: python -m zipapp packs a project into one runnable .pyz file

Python
~/tips/tip-python-zipapp-single-file-app

python3 -m zipapp myapp -o tool.pyz -m "cli:main" -p "/usr/bin/env python3"

Bundles code and dependencies into one executable file that needs only python installed.

For a small internal tool I like shipping one file that runs on any machine with python.

python3 -m pip install -r requirements.txt --target myapp
python3 -m zipapp myapp -o tool.pyz -m "cli:main" -p "/usr/bin/env python3"
./tool.pyz --help

The first line installs dependencies into the myapp directory next to your code. zipapp zips that directory, -m cli:main names the function to run and -p adds a shebang so the file is executable. Pure python packages work well. Compiled extensions cannot be loaded from a zip file, so packages like numpy do not fit this approach. Run it with python3 tool.pyz if the file is not marked executable.

Further reading: zipapp documentation

More Python tips import asyncio from enum import StrEnum, auto except* ValueError as eg: import pytest pytest --maxfail=2 All Python tips (107) All topics

Link