Virtual environments

Virtual environments let you partially capture your computing environment: packages that you used and their versions, the Python and R binaries that you used, etc. The two main benefits coming from using virtual environments are isolation and an ability to recreate an environment:

  • By using a separate isolated virtual environment for each of your projects, you can safely install/remove/update any packages for one project without it affecting other projects.

  • You, your colleague, or anyone else for that matter, can later recreate your environment vastly increasing the chance of the reproducibility of your project. All you need to do is keep the file that is capturing the state of your environment after every change you make, and then share that file.

For smaller/shorter projects use renv for R, venv for Python. For anything more long-term, use conda, Docker, Singularity.

Python - `venv`

For Python, we will use venv because it comes with the standard library.

Create

python -m venv .venv

The second venv prepended by a dot - .venv is the name of the folder where the virtual environment will be stored. It does not have to be named that. Some other frequently used names are .env, env, venv. If you decide to use something other than .venv, update the commands below accordingly.

Activate

source .venv/bin/activate

Update

pip install numpy
pip freeze > requirements.txt

Restore

Delete .venv directory first if you already have one.

python -m venv .venv
pip install -r requirements.txt

Last updated