Zipapps
Package your python code into an executable zip file (with the requirements). Based on pure python zipapp, for Python3.6+.
Install / Use
/learn @ClericPy/ZipappsREADME
zipapps
zipapps is a pure-python library, to package your python code into a single zip file with its dependencies.
Depends on PEP441 + zipapp + zipimport.
What is the .pyz?
.pyz to Python is like .jar to Java.
They are both zip archive files which aggregate many packages and associated metadata and resources (text, images, etc.) into one file for distribution.
All you need is the Python Interpreter as the runtime environment.
So, what could zipapps be?
packaging toolcompress your package folder into a zip file.
single-file virtual environmenta portable site-packages.
dependences installerlazy install requirements while first running.
set of import-pathadd many
venv.pyzfiles to PYTHONPATH automatically.requirements.txt freezing toolkituse
venvto freeze your pip packages.
PS: The pyz extension can be modified to any character you want, such as
.pyand.zip.
Install & Quick Start
pip install zipapps -U
1. Source code without dependences
- add your source code into the zip file, and set the entrypoint with
-m-
python3 -m zipapps -c -a entry.py -m entry:main -o app.pyz
entry.pycould be a packagepackage_name-mformat ispackage.module:function
-
- run app.pyz
-
python3 app.pyz
-
2. Source code with dependences
-
zipapps with requirements
-
python3 -m zipapps -c -a entry.py -m entry:main -o app.pyz bottle
bottleis a pure-python lib which may not be unzipped-u AUTOor-u=*should be set for some.so/.pydlibs- such as
psutil - notice that the
-u=*!=-u *
- such as
-
you don't need to install requirements at running
- ensure the compatibility of the system environment and python version
- or add
-doflazy installmode to skip the compatibility problem- but comes a long-time
pip installprocess at the first running.
- but comes a long-time
-
-
run app.pyz
-
python3 app.pyz
- libs with
.pyd/.socaches will be unzipped to the./zipapps_cache/appby-u AUTO
-
3. Virtual environments
-
zipapps with requirements
-
python3 -m zipapps -c -u AUTO -o venv.pyz -r requirements.txt
-
-
run entry.py with venv.pyz
-
python3 venv.pyz entry.py
- the cache will be unzipped to
./zipapps_cache/venvfor-uis not null
-
4. Activate the .pyz environment
- use
importlib(Recommended)sys.path.insert(0, "some_lib_venv.pyz");importlib.import_module("ensure_zipapps")- automatically unzip cache, and add the path to sys.path
- it can be run multiple times
- if they are all pure-python code and no need to decompress
impory sys; sys.path.insert(0, "bottle.pyz")
- use
zipimport
# 1. use zipapps lib
from zipapps.activate_zipapps import activate
activate("app.pyz")
# or use source code directly
from zipimport import zipimporter
importer = zipimporter("app.pyz")
try:
spec = importer.find_spec("ensure_zipapps")
if spec and spec.loader:
module = spec.loader.load_module("ensure_zipapps")
else:
raise ImportError("Module not found")
except AttributeError:
module = importer.load_module("ensure_zipapps")
del module
sys.modules.pop("ensure_zipapps", None)
Command line usage

Build Args
most common args:
-c- to compress files, only for python3.7+.
-a xxx.py- to add some files/folders into the zipped file.
-u=AUTO- Recommended
- auto unzip the .pyd / .so files
- or
-u=*to unzip all the files
- or
-r requirements.txt- install requirements with
pip install
- install requirements with
-o my_app.pyz- output the zipped file as given path
-m app.__main__:main- entry point
-p /usr/bin/python3- set the
shebangline
- set the
-d- Recommended
- requirements will be installed with
pipwhile first running in the lazy install mode - zip file size will be very small, and the default unzip path is
SELF/zipapps_cache/
--clear-zipapps-cache,-czc- Clear the zipapps cache folder after running, but maybe failed for .pyd/.so files.
Details:
python3 -m zipapps -h
-h, --help- show the simple doc
--includes, --add, -a- The given paths will be copied to
cache_pathwhile packaging, which can be used while running. The path strings will be splited by ",".- such as
my_package_dir,my_module.py,my_config.json - often used for libs not from
pypior some special config files
- such as
- the
outputarg ofzipapps.create_app
- The given paths will be copied to
--output, -o- The path of the output file, defaults to
app.pyz. - the
outputarg ofzipapps.create_app
- The path of the output file, defaults to
--python, -p- The path of the Python interpreter which will be set as the
shebang line, defaults toNone.- with shebang
/usr/bin/python3you can run app with./app.pyzdirectly, no need forpython3 app.pyz
- with shebang
- the
interpreterarg ofzipapps.create_app
- The path of the Python interpreter which will be set as the
--main, -m- The entry point function of the application, the
valid formatis:package.module:functionpackage.modulemodule:functionpackage
- the
mainarg ofzipapps.create_app - WARNING: If the
--mainarg is set,python3 app.pyzwill not be able to used as venv likepython3 app.pyz xxx.py
- The entry point function of the application, the
--compress, -cBooleanvalue, compress files with the deflate method or not.- the
compressedarg ofzipapps.create_app
--unzip, -u- The names which need to be unzipped while running, splited by ","
without ext, such asbottle,aiohttp, or the complete path likebin/bottle.py,temp.py. For.so/.pydfiles(which can not be loaded by zipimport), or packages with operations of static files.- if unzip is set to "*", then will unzip all files and folders.
- if unzip is set to AUTO, then will add the
.pydand.sofiles automatically.
- Can be overwrite with environment variable
ZIPAPPS_UNZIP - the
unziparg ofzipapps.create_app
- The names which need to be unzipped while running, splited by ","
--unzip-exclude, -ue- The opposite of
--unzip/-uwhich will not be unzipped, should be used with--unzip/-u. - Can be overwrite with environment variable
ZIPAPPS_UNZIP_EXCLUDE
- The opposite of
--unzip-path, -up3. Ifunziparg is not null, cache files will be unzipped to the given path while running. Defaults tozipapps_cache, support some internal variables as runtime args:$TEMP/$HOME/$SELF/$PID/$CWDas prefix, for example$HOME/zipapps_cacheTEMPmeanstempfile.gettempdir()HOMEmeansPath.home()SELFmeans.pyzfile path.$PIDmeansos.getpid()$CWDmeansPath.cwd()
- And you can also overwrite it with environment variables:
ZIPAPPS_CACHEorUNZIP_PATH
- the
unzip_patharg ofzipapps.create_app
-cc, --pyc, --compile, --compiled- Compile .py to .pyc for fast import, but zipapp does not work unless you unzip it(so NOT very useful).
- the
compiledarg ofzipapps.create_app
--cache-path, --source-dir, -cp- The cache path of zipapps to store site-packages and
includesfiles. If not set, will create and clean-up in TEMP dir automately. - the
cache_patharg ofzipapps.create_app
- The cache path of zipapps to store site-packages and
--shell, -s- Only while
mainis not set, used for shell=True insubprocess.run.- very rarely used, because extra sub-process is not welcome
- the
shellarg ofzipapps.create_app
- Only while
--main-shell, -ss- Only for
mainis not null, callmainwithsubprocess.Popen:python -c "import a.b;a.b.c()". This is used forpsutilImportError of DLL load.- very rarely used too
- the
main_shellarg ofzipapps.create_app
- Only for
--strict-python-path, -sppBooleanvalue. Ignore global PYTHONPATH, only usezipapps_cacheandapp.pyz.- the
ignore_system_python_patharg ofzipapps.create_app
-b, --build-id- The string to skip duplicate builds, it can be the paths of files/folders which splited by ",", then the modify time will be used as build_id. If build_id contains
*, will useglobfunction to get paths. For example, you can set requirements.txt as your build_id bypython3 -m zipapps -b requirements.txt -r requirements.txtwhen you use pyz as venv.- very rarely used too too
- the
build_idarg ofzipapps.create_app
- The string to skip duplicate builds, it can be the paths of files/folders which splited by ",", then the modify time will be used as build_id. If build_id contains
--zipapps, --env-paths- Default
--zipappsarg if it is not given while running. Support $TEMP/$HOME/$SELF/$PID/$CWD prefix.
- Default
--delay, -d, --lazy-pip, --lazy-install, --lazy-pip-install- Install packages with pip while first running, which means requirements will not be install into pyz file.
--ensure-pip- Add the ensurepip package to your pyz file, works for embed-python(windows) or other python versions without
pipinstalled but `lazy-install
- Add the ensurepip package to your pyz file, works for embed-python(windows) or other python versions without
