Bundle python script and dependencies into a single file

耗尽温柔 提交于 2019-11-27 15:14:13

You might want to consider looking at Twitter's PEX library which can create executable files from python packages: https://pex.readthedocs.org/en/latest/whatispex.html

.pex files are just carefully constructed zip files with a #!/usr/bin/env python and special __main.py__

Update 2016: wagon helps building wheel packages with dependencies for offline installation.


For simple projects keeping all source together in one folder and copying it as a whole is good enough. You can use git to push your code to a central repository and pull it to your server, without building any packages. Fabric and Ansible are two tools that can help you automate the deployment process. (For example, remotely run git pull and delete all your pyc files).

If you have shared dependencies between projects, pip and wheels are the modern alternatives to eggs:

You can create a simple bundle that contains all of the dependencies you wish to install using.

$ tempdir=$(mktemp -d /tmp/wheelhouse-XXXXX)
$ pip wheel -r requirements.txt --wheel-dir=$tempdir
$ cwd=`pwd`
$ (cd "$tempdir"; tar -cjvf "$cwd/bundled.tar.bz2" *)

Once you have a bundle, you can then uninstall it using:

$ tempdir=$(mktemp -d /tmp/wheelhouse-XXXXX)
$ (cd $tempdir; tar -xvf /path/to/bundled.tar.bz2)
$ pip install --force-reinstall --ignore-installed --upgrade --no-index --use-wheel --no-deps $tempdir/*

(From pip docs)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!