package python scripts and dependencies in single files with pex

02 Oct 2018

So you want to create a pex that packages your script and its dependencies? Ok, first to make our script! call it

import requests

if __name__ == '__main__':
  req = requests.get("")
  print req.text.split("\n")[0]



Now, it’s time to package it!:

$ pex -o my-script.pex -D . -r requirements.txt -e my-script

Done, but wait, are you too lazy to even download pex/pip?, try docker:


#FROM python:3.6.4
FROM ubuntu:16.04

RUN  apt-get update && apt-get install -y libev-dev python-pip

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY requirements.txt /usr/src/app/
RUN  pip install --no-cache-dir -r requirements.txt

COPY . /usr/src/app

CMD [ "python", "" ]

And then:

$ docker build -t pex-builder .
$ docker run -v "$PWD:/usr/src/app" pex-builder \
    pex -o my-script.pex -D . -r requirements.txt -e my-script

Done, now we have a (relative) portable way of distributing and running our scripts:

$ ./my-script.pex

That’s it, happy packaging 😊