Skip to content

Instantly share code, notes, and snippets.

@simeonf simeonf/pex.md
Last active May 8, 2019

Embed
What would you like to do?
Creating a PEX from a python script

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...
@eddiewebb

This comment has been minimized.

Copy link

commented Apr 12, 2018

Thank you! this is super helpful. My main script works, but complains it can't find other local files.

queueBuildUntilFrontOfLine.py

#!/usr/bin/env python
import sys
import time
import circleci #local file
import environment #local file

do stuff...

layout

 ls -l src/*.py
-rwxr-xr-x  1 n0158588  staff  1419 Apr 12 12:50 src/queueBuildUntilFrontOfLine.py
-rw-r--r--  1 n0158588  staff  1131 Apr 12 12:50 src/circleci.py
-rw-r--r--  1 n0158588  staff   209 Apr 12 08:47 src/environment.py
-rw-r--r--  1 n0158588  staff   161 Apr 12 15:07 src/setup.py

python foo.py works fine, but the packaged pex fails.

pex src requests -c queueBuildUntilFrontOfLine.py -o queueBuildUntilFrontOfLine.pex -f src
./queueBuildUntilFrontOfLine.pex

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

@kneufeld

This comment has been minimized.

Copy link

commented Apr 15, 2018

If you find the answer please post it here. I find the docs for pants and pex to be terrible. There is no single complete example and all the descriptions are overly technical and examples too simple.

@wangshan

This comment has been minimized.

Copy link

commented Apr 19, 2018

try to make circleci.py a module, ie, put it in a directory like this:

circleci
  |__ circleci/circleci.py
  |__ circleci/__init__.py
  |__ setup.py

in setup.py write this:

from distutils.core import setup
setup(name='circleci',
    version='1.0',
    packages=['circleci'],
    )
@archenroot

This comment has been minimized.

Copy link

commented May 8, 2019

By following the original content I get:
Unable to parse /home/zangetsu/.pex/install/foo-1.0-py3-none-any.whl.ee5c21cd6d1cd79925b2f4a751c96970ec7248cf/foo-1.0-py3-none-any.whl/bin/foo.py. PEX script support only supports Python scripts.

@archenroot

This comment has been minimized.

Copy link

commented May 8, 2019

Ok, issue with Python 3.6 and print function change....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.