Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hamelin/3ca2c4c4767cbdb21c07694cf970c48a to your computer and use it in GitHub Desktop.
Save hamelin/3ca2c4c4767cbdb21c07694cf970c48a to your computer and use it in GitHub Desktop.
Building and packaging Python Wheels for offline installation
#!/bin/bash
# This script assumes the virtual env (or Conda environment) to install the wheels in has already been activated.
# Expected arguments: path to the Tar archive built with script package-wheels.sh
set -e
PATH_TARFILE="$(realpath "$1")"
DIR_WORK="$(mktemp -d)"
HERE="$(pwd)"
cd "$DIR_WORK"
tar xf "$PATH_TARFILE"
pip install --no-index --find-links ./wheels $(xargs < requirements.txt)
cd $HERE
rm -rf $DIR_WORK
#!/bin/bash
# Expected arguments: description of desired packages, as one would write when invoking pip install
set -e
DIR_WORK="$(mktemp -d)"
HERE="$(pwd)"
cd "$DIR_WORK"
echo $@ > requirements.txt
mkdir wheels
python -m venv bare-env
. bare-env/bin/activate
pip wheel -w ./wheels $@
deactivate
tar cf "$HERE/wheels-$(date +%Y%M%d-%H%M).tar" requirements.txt wheels
cd $HERE
rm -rf $DIR_WORK

These two scripts demonstrate the ideas backing my approach for putting together files that enable offline deployment of a Python computing environment. I don't strictly promise that they run without tweaking.

The packaging has to be done on a system similar enough to the target system for installation. For instance, if one intends to deploy the environment on a Intel 64-bit CPU system (x86-64 architecture) running Linux, run package-wheels.sh on a x86-64 Linux box that has Internet access. No need for strict version matching of kernels and LibC, unless the target system is much older than the one I'm packaging from. Virtual machines are useful for packaging, as are temporary cloud nodes. Myself, if I find myself without a system at hand that matches the target, I love using Digital Ocean for quick VM instantiation and tear down: I run my packaging, transfer the archive of wheels to my local computer, and boom! done. For the 30 minutes the whole thing might take, the cost I incur amounts to a few cents, and is much quicker than building a Linux box using, say, VirtualBox or VMWare.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment