Skip to content Skip to sidebar Skip to footer

Bundle Python Script And Dependencies Into A Single File

I have a few script that had their own copy of a some functions, so I extracted these functions to a module and had the scripts import the function. These script are to be copied o

Solution 1:

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__

Solution 2:

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)

Post a Comment for "Bundle Python Script And Dependencies Into A Single File"