Distributing Python virtual environments

Paul Moore p.f.moore at gmail.com
Thu Mar 29 10:39:45 EDT 2018


On 29 March 2018 at 15:06, Malcolm Greene <python at bdurham.com> wrote:
> We're using virtual environments with Python 3.6. Since all our pip
> installed modules are in our environment's local site-packages folder,
> is the distribution of a virtual environment as simple as recursively
> zipping the environment's root folder and distributing that archive to
> another machine running the same OS and same version of Python? The
> reason we would go this route vs downloading dependencies from a
> requirements.txt file is that the target machines may be in a private
> subnet with minimal access to the internet.

Typically that's not supported - wrapper scripts for example include
hardcoded path names that are specific to the original environment.
There's a --relocatable flag to the virtualenv command that (in
theory) tries to make the scripts use relative paths, but it's not
very reliable. Also, there are other potential risks in doing this -
as Chris said, you need to be very precise about architecture, etc.
Overall, I'd say this is a very risky approach.

A better approach would likely be to do "pip freeze" to generate a
requirements file (if you don't already have one), then "pip wheel -r
<requirements file>" to get a complete set of wheels for your
virtualenv. Then ship those wheels to the target machine (caveats
about exactly the same architecture still apply, as the wheels you
build may be architecture-specific), and build a new virtualenv and
install them with "pip install -r <requirements_file> --find-links
<directory_containing_the_wheels>".

Paul



More information about the Python-list mailing list