If I have a large set of dependencies that I want ...
# general
g
If I have a large set of dependencies that I want to shove into a PEX, but I want multiple with different entry points; is there a way to do this without duplicate the full size of the PEX? For example, I want a gunicorn and a celery PEX and both have the same exact set of deps. Rather than having two large PEX binaries that are ~400MB each, I desire to dedup them so my docker container remains as small as possible.
b
Instead of building 2 separate PEX files, could you build one file with both gunicorn and celery and specify the entry point at runtime instead via the
PEX_SCRIPT
or
PEX_MODULE
variables?
That’s basically what I do for this same use case
Here’s the set of runtime variables you can pass to pex: https://docs.pex-tool.org/api/vars.html#
πŸ™Œ 1
g
@better-van-82973 thank you
I think I will create wrapper scripts that sets those variables and then calls the pex. This is perfect. Thank you
Something like this?
Copy code
#!/bin/bash

export PEX_MODULE=gunicorn.app.wsgiapp:run

exec /usr/bin/local/my-pex-binary $@
b
Yeah, mine is actually a little shorter:
Copy code
PEX_MODULE=gunicorn.app.wsgiapp:run /bin/app/pex
πŸ‘ 1
c
there's also
conscript
if you have multiple entry point scripts to bake in a single pex: https://pypi.org/project/conscript/
πŸ™Œ 1
b
Another/related approach would be to split the dependencies and source up, and plop them into an image as separate layers, which has build-time benefits too. https://www.pantsbuild.org/blog/2022/08/02/optimizing-python-docker-deploys-using-pants
πŸ‘€ 1
a
If you dont specify an entrypoint when you build the pex it behaves like the python interpreter so you can just pass in the
-m entry_module
flag to it.
πŸ™Œ 1
eg
Copy code
(venv-demo) ➜  /tmp pex gunicorn flask -o my.pex     
(venv-demo) ➜  /tmp ./my.pex -m gunicorn --help | head
usage: __main__.py [OPTIONS] [APP_MODULE]

options:
  -h, --help            show this help message and exit
  -v, --version         show program's version number and exit
  -c CONFIG, --config CONFIG
                        :ref:`The Gunicorn config file<configuration_file>`.
                        [./gunicorn.conf.py]
  -b ADDRESS, --bind ADDRESS
                        The socket to bind. [['127.0.0.1:8000']]
(venv-demo) ➜  /tmp ./my.pex -m flask --help | head
Error: Could not locate a Flask application. Use the 'flask --app' option, 'FLASK_APP' environment variable, or a 'wsgi.py' or 'app.py' file in the current directory.

Usage: python -m flask [OPTIONS] COMMAND [ARGS]...

  A general utility script for Flask applications.

  An application to load must be given with the '--app' option, 'FLASK_APP'
  environment variable, or with a 'wsgi.py' or 'app.py' file in the current
  directory.

Options:
  -e, --env-file FILE   Load environment variables from this file. python-
(venv-demo) ➜  /tmp