Working with Python PEX Files

Suppose you have a package that imports a lot of weird modules and you’d like to share this package without forcing end users to install a bunch of stuff. You can bundle this into a PEX file and everyone will live happily ever after.

PEX Files

Well, it’s a carefully crafted zip file of egg files with a hashbang prepended onto it. I first learned about them in this YouTube video. Here is the documentation. If you open a PEX file without any arguments, it will open a Python interpreter with whatever modules it was bundled with. If you give it a Python script it will run that script with whatever modules it was bundled with.


I had to install wheels before I could install PEX.

pip install wheel
pip install pex


Suppose you have your own package with the following structure:

|  |
|  `

We’ll assume that your script looks like this,

#!/usr/bin/env python

from setuptools import setup, find_packages

    author="Mr. Coffee",
    description="A simple PEX example",

We’ll assume further that you’ve organized as a bunch of functions, a main() function and an if __name__=="__main__": statement, like this,

#!/usr/bin/env python

import weirdStuff

def fct_1():

def fct_2():

def main():

if __name__ == "__main__":

Then we can build a PEX file by requiring weirdStuff, sourcing the myStuff package, and executing the myStuff.main() by specifying that endpoint,

pex -r weirdStuff -s myStuff -e myStuff.myStuff:main -o out.pex

So, the -r flag requires weirdStuff, the -s flag sources your package, and the -e flag specifies the endpoint–that means that after weirdStuff and myStuff is imported, myStuff.myStuff.main() is called. Now you can shoot this guy to a colleague and they can execute it by calling ./out.pex at the command line, assuming that they have Python installed.

Leave a Reply

Your email address will not be published. Required fields are marked *