Motivation

Copying an environment

There is not a good and easy way to fully share a virtual environment across machines. The most convenient solution is first to create the environment and then re-install this environment in a new environment. A requirements file can be used for this cause. First we use pip freeze to save a list of requirements to file:

(venv2)Terminal> pip freeze > requirements.txt

If all the above steps in creating the venv2 environment have been followed, the requirements file should have the following content:

Django==1.4.1
argparse==1.2.1
wsgiref==0.1.2
yolk==0.4.3

In case one has also installed a Python package from the repository of a version control system, the particular commit version (and of course the URL) is recorded as data for that package.

To replicate exactly the same environment inside another Virtualenv environment, we create a new environment, say it is called venv3, copy the requirements.txt file to the venv3 environment, and use pip to install all the packages and their versions specified in requirements.txt at once:

(venv3)Terminal> pip install -r requirements.txt

A yolk -l command can be used to check the success of the multiple installations.

A word of caution is necessary here. Distributing a requirements.txt file produced by pip freeze will not always re-create an environment by a simple pip install -r requirements.txt. For example, the output from pip freeze may not account for the fact that some packages must be installed before others. When creating a Python environment for doing scientific computing, numpy is a package that must be installed before most other packages. Some packages can be challenging to compile via pip install (ScientificPython is an example, although manual execution of setup.py runs fine). Also, many Python scientific computing packages depend on much non-Python software that cannot be installed by pip. For such complex environments it is recommended to create a script that performs the manual installation tasks, but it can utilize pip to as large extent as possible.