So you want to create a pex that packages your script and its dependencies?
Ok - first to make our script! Call it foo.py:
import requests
if __name__ == '__main__':
req = requests.get("https://raw.githubusercontent.com/pantsbuild/pex/master/README.rst")
print req.text.split("\n")[0]
It's a requests
hello world program. To package it we need a minimal setup.py
for distributing a script. Put it
in the same directory:
from distutils.core import setup
setup(name='foo',
version='1.0',
scripts=['foo.py'],
)
Now that our script is packageable we can use pex
to build an executable out of it:
$ pex . requests -c foo.py -o foo.pex -f dist
Briefly - requests
and .
are our dependencies. -c foo.py
tells pex what to run. And -o foo.pex
creates the
output file.
You should get a foo.pex
file in the same directory. Run it to see the script execute:
$ ./foo.pex
PEX
And you can unzip the pex to see the contents including the packaged dependencies:
$ unzip -t foo.pex
... much output not shown...
Thank you! this is super helpful. My main script works, but complains it can't find other local files.
queueBuildUntilFrontOfLine.py
layout
python foo.py works fine, but the packaged pex fails.
ImportError: No module named circleci
Do you have any advice? I'm struglging to make sense of https://pex.readthedocs.io/en/stable/buildingpex.html#specifying-requirements