12.3. Module Zipapp

  • import zipapp - Create executable Python archives

  • Since Python 3.5

In order to be executed by Python, an application archive simply has to be a standard zip file containing a __main__.py file which will be run as the entry point for the application.

12.3.1. Creating Standalone Applications

  1. Create your application in a directory "myapp" as normal

  2. Create __main__.py file (this will be entrypoint)

  3. Install (using pip) all of your application's dependencies:

    $ python -m pip install -r requirements.txt --target myapp
    
  4. Package the application using:

    $ python -m zipapp -p "/usr/bin/env python3" myapp
    

12.3.2. About Standalone Applications

  • This will produce a standalone executable

  • It can be shipped to users as a single file

  • It can be run on any machine with the appropriate interpreter available

  • On Unix, the myapp.pyz file is executable as it stands

  • If you want you can rename the file to remove the .pyz extension

  • On Windows, the myapp.pyz[w] file is executable by virtue of the fact that the Python interpreter registers the .pyz and .pyzw file extensions when installed

12.3.3. Run Archive

$ python myapp.pyz

12.3.4. Show Archive

$ unzip -l myapp.pyz
Archive:  myapp.pyz
  Length      Date    Time    Name
---------  ---------- -----   ----
      149  01-30-2025 13:46   main.py
        0  01-30-2025 13:15   utils/
      184  01-30-2025 13:15   utils/__init__.py
       98  01-30-2025 12:46   utils/math.py
       56  01-30-2025 13:14   utils/strings.py
       50  01-30-2025 13:47   __main__.py
---------                     -------
      537                     6 files