Distributing Python Apps with ZipApps

2021-10-02

I've long wanted a good mechanism to distribute Python apps to users. I've looked into tools like PyInstaller and Nuitka, and after examining what youtube-dl was doing, I ran across a feature built into Python since 3.5 called zipapps. Zipapps are essentially scripts with a zip-based payload that's an archive of an "app" directory. This payload then gets passed to Python directly, which any version after 3.5 knows how to decompress and execute. In practice, I've found this to work really well with youtube-dl, so I was curious what a sort of "hello world" version of a zipapp would require. It turns out, not much!

Given a simple python app print("Hello, World!"), inject it into a file called __main__.py in a new directory:

mkdir hw
echo "print(\"Hello, world!\")" > hw/__main__.py

Then compile the directory using zipapp:

python -m zipapp -o hw.pyz -p /usr/bin/python3 hw/

Then simply run it: ./hw.pyz. On my laptop, it runs in about 25ms.