Package your python code into one zip file, even a virtual environment.
Project description
zipapps
Package your python code (with requirements) into a standalone zip file (like a jar
).
zipapps
is a pure-python library
, without any 3rd-party dependencies. Inspired by shiv but unlike shiv
, this lib will not always create new cache folders while running, and easy to combine multiple venv.pyz
files then let them work well together.
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. Then what you only need is a Python Interpreter as the runtime environment.
PS: The pyz ext could be any other suffixes even without ext names, so you can rename app.pyz
to app.zip
as you wish. Depends on PEP441, then the apps may be cross-platform
as long as written with pure python code without any C++ building processes.
When to Use it?
- Package your code(package or model) into one zipped file.
- sometimes togather with the requirements.
- Run with python interpreter from the venv path.
- which means the requirements(need to be unzipped) will be installed to the
venv
folder, not inpyz
file. - build your package into one
pyz
file with-m package.module:function -p /venv/bin/python
. - run the
pyz
file with/venv/bin/python app.pyz
or./app.pyz
.
- which means the requirements(need to be unzipped) will be installed to the
- Hadoop-Streaming's mapper & reducer scripts.
- Simple deployment towards different servers with
jenkins
, or other CI/CD tools.- Easy to uploads a clean
standalone
zip file.
- Easy to uploads a clean
- Distribute
zipapp
with embedded python. - Use as a requirements zip path, or some
venv
usages.import sys;sys.path.insert(0, 'app.pyz')
(without .so/.pyd)python3 app.pyz script.py
- Other usages need to be found, and enjoy yourself.
Install & Quick Start
pip install zipapps -U
zip as apps
- zipapps with requirements
-
python3 -m zipapps -u AUTO -a entry.py -m entry:main -o app.pyz aiohttp,psutils
-
- run app.pyz
-
python3 app.pyz
- libs with
.pyd/.so
caches will be unzipped to the./zipapps_cache/app
-
zip as virtual environments
- zipapps with requirements
-
python3 -m zipapps -u AUTO -o venv.pyz -r requirements.txt
-
- run entry.py with venv.pyz
-
python3 venv.pyz entry.py
- cache will be unzipped to
./zipapps_cache/venv
if -u is not null
-
How to Use?
Advance Usage
1. Package your script file with only built-ins functions.
1.1 Code of script.py
print('ok')
1.2 build the app.pyz
python3 -m zipapps -c -a script.py -m script -p /usr/bin/python3
1.3 run this app
python3 app.pyz
or
./app.pyz
ouput
ok
Details:
-c:
compress files, 7.6KB => 3.3KB
-a:
add the script file to app.pyz, so you can use it while running
-m:
set the entry_point, also can be set as `-m script:main` as long as you has the main function
-p:
set the shebang line, so you can use `./app.pyz` instead of `python3 app.pyz`
2. Package your package folder into one zip file.
2.1 The package example
and the code of __main__.py
└── example
├── __init__.py
└── __main__.py
def main():
print('ok')
if __name__ == "__main__":
main()
2.2 build the example.pyz
python3 -m zipapps -c -a example -o example.pyz -m example.main:main -p /usr/bin/python3
2.3 Run with python3 example.pyz
or ./example.pyz
output
ok
Details:
-m:
set the entry_point with format like `package.model:function`
-o:
set the output file path, you can set it some other paths like `/home/centos/example.abc` and run with `python3 /home/centos/example.abc`
no more new args.
3. Package your code with requirements (bottle).
3.1 The package example
and the code of __main__.py
└── example
├── __init__.py
└── __main__.py
def main():
import bottle
print(bottle.__version__)
if __name__ == "__main__":
main()
3.2 build the example.pyz
with requirements installed
python3 -m zipapps -c -a example -o example.pyz -m example.main:main bottle
3.3 Run with python3 example.pyz
or ./example.pyz
Output
0.12.19
Details:
bottle:
all the unhandled args like `bottle` will be used to `pip install`, so you can write `bottle` in requirements.txt and use like `-r requirements.txt`
WARNING: if the requirements have .pyd/.so
files, you should unzip them while running, and the pure python libs like bottle
or requests
no need to unzip anything. Read the 4th paragraph for more info.
4. Package your code with the requirements which includes .pyd/.so
files.
4.1 The package example
and the code of __main__.py
└── example
├── __init__.py
└── __main__.py
def main():
import psutil
print(psutil.__version__)
if __name__ == "__main__":
main()
4.2 build the example.pyz
with requirements installed
python3 -m zipapps -c -a example -o example.pyz -m example.main:main -u AUTO -up TEMP/cache psutil
4.3 Run with python3 example.pyz
or ./example.pyz
Output
5.8.0
Details:
-u:
means the file or folder names you want to unzip while running. Here is the `AUTO`, will unzip the psutil package because of its .pyd or .so files included.
-up:
the unzip path of cache folder. TEMP / HOME / SELF are the built-in runtime args, but for the stable usage you can ignore this arg then use `./zipapps_cache/example`. The cache will be refreshed while you rebuild this pyz.
WARNING: unzip path can be overwrited by export ZIPAPPS_CACHE=./xxx
or export UNZIP_PATH=./xxx
while running.
5. Package the requirements like a virtual environment without entry_point.
5.1 Code of script.py
import bottle, requests
print(bottle.__version__, requests.__version__)
5.2 build the venv.pyz
python3 -m zipapps -c -o venv.pyz -p /usr/bin/python3 bottle requests
5.3.1 use the venv.pyz
like a middleware
python3 venv.pyz script.py
5.3.2 use the venv.pyz
like the interpreter
./venv.pyz script.py
even like is also valid
python3 venv.pyz -c "import bottle,requests;print(bottle.__version__, requests.__version__)"
Output
0.12.19 2.25.1
Details:
No `-m` arg here, then the pyz file will do like an interpreter which contains the installed requirements.
So you can use it like this:
> python3 venv.pyz
>>> import bottle
>>> bottle.__file__
6. Using multiple venv pyz files for your pyz model.
6.1 Here is script.py
again
import bottle, requests
print(bottle.__version__, requests.__version__)
6.2 Build the script.pyz
python3 -m zipapps -c -o script.pyz -a script.py -m script requests
6.3 Build the requests.pyz
and bottle.pyz
respectively
python3 -m zipapps -c -o requests.pyz requests
python3 -m zipapps -c -o bottle.pyz bottle
6.4 And now run the script.pyz
with two requirements
python3 script.pyz --zipapps=bottle.pyz,requests.pyz
Output
0.12.19 2.25.1
Details:
--zipapps:
This arg will help you run some zipapp with given venv.pyz files, the paths is separated by commas.
Command line args
Packaging 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
- auto unzip the .pyd / .so files
-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
- set the entry point
-p /usr/bin/python3
- set the
shebang
line
- set the
Details:
-h, --help
- show the simple doc
--includes, --add, -a
- The given paths will be copied to
cache_path
while 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
pypi
or some special config files
- such as
- the
output
arg ofzipapps.create_app
- The given paths will be copied to
--output, -o
- The path of the output file, defaults to
app.pyz
. - the
output
arg 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/python3
you can run app with./app.pyz
directly, no need forpython3 app.pyz
- with shebang
- the
interpreter
arg 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 format
is:package.module:function
package.module
module:function
package
- the
main
arg ofzipapps.create_app
- WARNING: If the
--main
arg is set,python3 app.pyz
will not be able to used as venv likepython3 app.pyz xxx.py
- The entry point function of the application, the
--compress, -c
Boolean
value, compress files with the deflate method or not.- the
compressed
arg 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/.pyd
files(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
.pyd
and.so
files automatically.
- the
unzip
arg ofzipapps.create_app
- The names which need to be unzipped while running, splited by ","
--unzip-path, -up
- If
unzip
arg 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
as prefix, for exampleHOME/zipapps_cache
TEMP
meanstempfile.gettempdir()
HOME
meansPath.home()
SELF
means.pyz
file path.
- And you can also overwrite it with environment variables:
ZIPAPPS_CACHE
orUNZIP_PATH
- the
unzip_path
arg ofzipapps.create_app
- If
-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
compiled
arg ofzipapps.create_app
--cache-path, --source-dir, -cp
- The cache path of zipapps to store site-packages and
includes
files. If not set, will create and clean-up in TEMP dir automately. - the
cache_path
arg ofzipapps.create_app
--shell, -s
- Only while
main
is not set, used for shell=True insubprocess.run
.- very rarely used, because extra sub-process is not welcome
- the
shell
arg ofzipapps.create_app
- Only while
--main-shell, -ss
- Only for
main
is not null, callmain
withsubprocess.Popen
:python -c "import a.b;a.b.c()"
. This is used forpsutil
ImportError of DLL load.- very rarely used too
- the
main_shell
arg ofzipapps.create_app
- Only for
--strict-python-path, -spp
Boolean
value. Ignore global PYTHONPATH, only usezipapps_cache
andapp.pyz
.- the
ignore_system_python_path
arg 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 useglob
function to get paths. For example, you can set requirements.txt as your build_id bypython3 -m zipapps -b requirements.txt -r requirements.txt
when you use pyz as venv.- very rarely used too too
- the
build_id
arg 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
--zipapps
arg if it is not given while running. Support TEMP/HOME/SELF 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.
- all the other (or
unknown
) args will be used by "pip install"- such as
-r requirements.txt
- such as
bottle aiohttp
- the
pip_args
arg ofzipapps.create_app
- such as
Runtime args
available args while the
.pyz
is running
--zipapps
- including some other pyz into PYTHONPATH
- often be used as
multiple venv combination
- for example
- building
python3 -m zipapps -o six.pyz six
python3 -m zipapps -o psutil.pyz -u AUTO psutil
python3 -m zipapps -o bottle.pyz bottle
- run
python3 six.pyz --zipapps=psutil.pyz,bottle.pyz -c "import psutil, bottle"
- building
Changelogs
- 2021.04.22
- update the lazy_install mode (
-d
)- simple use case:
-
python -m zipapps -d six
-
python app.pyz -c "import six;print(six.__file__)"
-
- it will not
rmtree
the_zipapps_lazy_pip
folder for new build- if you need to upgrade those requirements, use
-U
arg of pip - lazy_install mode will install requirements with
pip install
while first running- so you can just run like this to init its installation(
-m
not set):python3 app.pyz -V
- so you can just run like this to init its installation(
- if you need to upgrade those requirements, use
- for now,
-d
is to server for the cross-platform and cross-python-version app- pip target path is separated from different python version and system platform
- python version accurity can be reset with
-pva 5
, defaults to 2, means 3.8.3 and 3.8.4 are same3.8
- python version accurity can be reset with
- pip target path is separated from different python version and system platform
- simple use case:
- update the lazy_install mode (
- 2021.04.11
- add
--sys-paths
to include new paths of sys.path- support TEMP/HOME/SELF prefix, separated by commas
- sometimes be used for separating pyz code from requirements
- requirements often be installed by
pip install xxx -t $SOME_PATH
- requirements often be installed by
- add
- 2021.04.01
- use
ensurepip
instead of installpip
while running withlazy-install
unzip_path
has been set toSELF/zipapps_cache
by default whenlazy_install
isTrue
- use
- 2021.03.31
- use
sys.stderr.write
instead ofwarnings.warn
- support
-d
for lazy pip install- example:
python3 -m zipapps -d bottle -r requirements.txt
- the
bottle
and other packages ofrequirements.txt
will be install while first running - this is useful for cross-platform distributions, which means
pyz
file size is much smaller
- example:
- use
- 2021.03.02
- fix auto-unzip nonsense folders while
-u AUTO
but no need to unzip any files
- fix auto-unzip nonsense folders while
- 2021.01.29
- fix packaging zipapps self
python3 -m zipapps -m zipapps.__main__:main -a zipapps -o zipapps.pyz
- add
zipapps.pyz
torelease
page
- fix packaging zipapps self
- 2021.01.28
- fix bug: run
.py
file with run_path missing sys.argvpython3 app.pyz xxx.py -a -b -c
- fix bug: run
- 2021.01.11
- add
--zipapps
arg while building pyz files- to activate some venv pyz with given paths while running it
- also support
TEMP/HOME/SELF
prefix, these internal variables are still runtime args.
- add
- 2020.12.27
- Combile multiple
pyz
files, do like this:- python3 -m zipapps -o six.pyz six
- python3 -m zipapps -o psutil.pyz -u AUTO psutil
- python3 six.pyz --zipapps=psutil.pyz -c "import six,psutil;print(six.file, psutil.file)"
- Combile multiple
- 2020.12.23
--unzip
support auto-check by-u AUTO
, alias for--unzip=AUTO_UNZIP
- fix
run_module
bug while running./app.pyz -m module
- 2020.12.21
- now will not run a new subprocess in most cases.
- using
runpy.run_path
andrunpy.run_module
- and using
subprocess.run
instead ofsubprocess.call
- using
- now will not run a new subprocess in most cases.
- 2020.12.13
--unzip
support complete path--unzip
support auto-check by--unzip=AUTO_UNZIP
- 2020.11.23
- add
activate_zipapps
to activate zipappsPYTHONPATH
easily
- add
- 2020.11.21
- reset unzip_path as the parent folder to unzip files
- so the cache path will be like
./zipapps_cache/app/
forapp.pyz
, - this is different from old versions.
- so the cache path will be like
- add environment variable
ZIPAPPS_CACHE
for argunzip_path
- add environment variable
ZIPAPPS_UNZIP
for argunzip
- reset unzip_path as the parent folder to unzip files
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file zipapps-2021.4.22-py3-none-any.whl
.
File metadata
- Download URL: zipapps-2021.4.22-py3-none-any.whl
- Upload date:
- Size: 16.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.0.3 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5bf5fced33d23ae4b37477b9677070ac28329601073ae098cb85db67d8d5674d |
|
MD5 | bb63b0175d10539e17c3cc9c68aed27c |
|
BLAKE2b-256 | 288c04fc1a3c6cb15828c935733a3cba7c112096d66416476df0995465917cce |