virtualenv is a tool to create Python isolated environments. It can be used to “freeze” modules versions and setups in order to simplify development and software distribution.

You can find extensive documentation about it online. This is just a series of notes about its usage for modules development, for quick reference.

Why?

Recently I came across rocker and off-your-rocker, two Python modules that simplify the usage of Docker with applications that needs GUI and GPU processing.

With virtualenv I have been able to modify the code without modifying the Python installation on my system and in a controlled environment.

I needed a quick reference on how to proceed, so I decided to write my own quick reference ;).

Preliminary steps

Install virtualenv if not already installed:

pip3 install virtualenv

Let’s create the project folder and a folder for our virtual environment:

mkdir -p ~/devel/off-your-rocker
mkdir -p ~/devel/off-your-rocker/virtualenv

Clone the repository

cd ~/devel/off-your-rocker
git clone https://github.com/sloretz/off-your-rocker.git 

Create the virtual environment

python3 -m venv ~/devel/off-your-rocker/virtualenv

Activate the virtual environment

When we want to use the configuration of the virtual environment, we have to “activate” it:

cd ~/devel/off-your-rocker/virtualenv
. ~/rocker_venv/bin/activate

In this shell, all the commands will use the Python environment isolated by virtualenv.

Install dependencies in the virtual environment

We may want to install dependencies in this virtual environment. The operations are the same of a classical Python installation. In this case:

pip install git+https://github.com/osrf/rocker.git
pip install git+https://github.com/sloretz/off-your-rocker.git

Install a local version of a module

We have edited our module and we want to test it. We have to install it from the local version:

# From the virtual environment
pip install ../off-your-rocker

From now on, any program that will run in the virtual environment shell will use the new version of the module.

Exit the virtual environment

If we want to close the virtual environment without closing the shell:

deactivate