Using pex to create standalone Python commands

Posted on Sun 15 January 2017

There's a little known tidbit about the Python interpreter in that it can execute a compressed archive if you put all the pieces in the right places. If you put a Python shebang in the head of that compressed archive then most shells will execute it without you needing to explicitly pass the archive into the Python interpreter.

Pex takes advantage of this and provides some tooling to allow you to take some arbitrary Python, all it's dependencies, expose an entry point and zip it all it one single big executable Python executable archive.

By using pex, we can take Python packages such as httpie, and wrap it into it's own standalone executable rather than having it pollute our system's Python site-packages directory.

Here's a simple script that helps us do all of that, put it at ~/bin/pexify and add ~/bin to your path, then simply invoke with pexify httpie http and it will create a self contained httpie executable at ~/bin/http, brilliant!

#!/bin/bash

# pexify
# usage: pexify package command
# where package: the python package to install
#       command: the command to take from that python package
#                and also the name of the output file

pip install $1 > /dev/null
pex $1 -c $2 -o ~/bin/$2
pip uninstall -y $1 > /dev/null

There are some caveats with this technique though, it is a bit of a hack and some packages (such as magic-wormhole) don't quite work.

Checkout the pex documentation if the idea of self contained executable archives appeal to you.


Comments !