Skip to content

Instantly share code, notes, and snippets.

Forked from wickman/
Created Jan 23, 2014
What would you like to do?

Pex.pex: Usage

PEX files are single-file lightweight virtual Python environments.

pex.pex is a utility that:

  • creates PEX files
  • provides a single use run-environment


See this doc: [[pex.pex.install|pants('src/python/twitter/common/python:pexinstall')]]


Usage: pex.pex [options]

pex.pex builds a PEX (Python Executable) file based on the given specifications: sources, requirements, their dependencies and other options

  --version             show program's version number and exit
  -h, --help            show this help message and exit
  --pypi, --no-pypi     Whether to use pypi to resolve dependencies; Default:
                        use pypi
                        The local cache directory to use for speeding up
                        requirement lookups; Default: ~/.pex/install
  -p PEX_NAME, --pex-name=PEX_NAME
                        The name of the generated .pex file: Omiting this will
                        run PEX immediately and not save it to a file
  -e ENTRY_POINT, --entry-point=ENTRY_POINT
                        The entry point for this pex; Omiting this will enter
                        the python IDLE with sources and requirements
                        available for import
                        requirement to be included; may be specified multiple
  --repo=PATH           Additional repository path (directory or URL) to look
                        for requirements.
  -s DIR, --source-dir=DIR
                        Source to be packaged; This <DIR> should be a pip-
                        installable project with a
  -v, --verbosity       Turn on logging verbosity.

Use cases

  • An isolated python environment containing your requirements and its dependencies for one time use

      pex.pex --requirement fabric
      >>> import fabric
      >>> ^D
  • A PEX file encapsulating your requirements and its dependencies for repeated use and sharing

      pex.pex --requirement fabric --pex-name my_fabric.pex
      >>> import fabric
      >>> ^D
  • A PEX file encapsulating your requirements intended to be run with a specific entry point

      pex.pex --requirement fabric --entry-point fabric.main:main --pex-name my_fabric.pex
      ./my_fabric.pex -h
      Usage: fab [options] <command>[:arg1,arg2=val2,host=foo,hosts='h1;h2',...] ...
        -h, --help            show this help message and exit
        -d NAME, --display=NAME
                              print detailed info about command NAME
      PEX_INTERPRETER=1 ./my_fabric.pex
      >>> import fabric
      >>> ^D

Bootstrapping pex.pex

  • Download virtualenv and create twitter.common.python virtualenv

      curl -O
      tar zxf virtualenv-1.11.tar.gz
      python virtualenv-1.11/ twitter.common.python.venv
  • Activate and install twitter.common.python

      cd twitter.common.python.venv
      source bin/activate
      pip install twitter.common.python
  • Use bin/pex to bootstrap itself

      pex -r twitter.common.python -e twitter.common.python.bin.pex:main -p pex.pex
  • Copy and deactivate virtualenv

      cp pex.pex ~/bin/pex
  • Use pex utility

      # Presumes that ~/bin is on $PATH
      pex -r fabric -e fabric.main:main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment