2

I have a Python application that comes with a command line script. I expose the script via setuptools "entry point" feature. Whenever a user runs the script, I would like the environment to be consistent with the package's requirements.txt. This means that the environment must contain versions of each dependency package that match the version specifiers in requirements.txt.

I know that this can be achieved with venv/virtualenv by making my users create a virtual environment, install requirements.txt in it, and activate that virtual environment whenever they run the script. I do not want to impose this burden of manually invoking virtualenv on users. Ruby's bundler solves this problem by providing bundler/setup-- when loaded, it modifies Ruby's $LOAD_PATH to reflect the contents of the Gemfile (analogue of requirements.txt). Thus it can be placed at the top of a script to transparently control the runtime environment. Does Python have an equivalent? That is, a way to set the environment at runtime according to requirements.txt without imposing additional complexity on the user?

Sean Mackesey
  • 10,701
  • 11
  • 40
  • 66

2 Answers2

1

I don't see why it wouldn't be possible for a Python program to install its own dependencies before importing them, but it is unheard of in the Python community.

I'd rather look at options to make your application a standalone executable, as explained here, for instance.

Markus Unterwaditzer
  • 7,992
  • 32
  • 60
  • Thanks Markus. Let me clarify: I do not need the script to *install* the dependencies when run. I am OK with having my users do a one-time `pip install --requirements.txt` after they have downloaded the package. The issue is future stability. The app uses specific versions of specific packages. I need to make sure that, after installation, the executable will indefinitely run in an environment with those specific versions, even if a user updates those packages. The solution should also be cross-platform (Windows, Mac, Linux). I will look into `py2exe` but I'm hoping for a simpler solution. – Sean Mackesey Jul 28 '16 at 19:08
  • [certbot](https://github.com/certbot/certbot) (née Letsencrypt) installs its own dependencies in a virtualenv when run, though technically you're running a shell script that does the installation and then calls the actual program. – jwodder Jul 28 '16 at 21:50
1

Does Python have an equivalent? That is, a way to set the environment at runtime according to requirements.txt without imposing additional complexity on the user?

Yes, more than one.

One is pex

pex is a library for generating .pex (Python EXecutable) files which are executable Python environments in the spirit of virtualenvs.

and the other is Platter:

Platter is a tool for Python that simplifies deployments on Unix servers. It’s a thin wrapper around pip, virtualenv and wheel and aids in creating packages that can install without compiling or downloading on servers.

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366