Virtual Environments on the Raspberry Pi

When using the latest version of Raspbian Stretch, a variable named VIRTUALENVWRAPPER_PYTHON must be set to the path of the python version that you want to use when creating virtual environments. You can create python virtual environments that use python2.7, python3.5, python3.6 or python3.7. We will use python3.5.

Set the variable VIRTUALENVWRAPPER_PYTHON
First you need to find the path of the version of python that you want to use. You can find this path by opening a Terminal window and typing:

which python 3.5

The terminal responds with: /usr/bin/python3.5
In the hidden file .profile, set the value for VIRTUALENVWRAPPER_PYTHON. We use the nano editor to do this.

sudo nano ~/.profile

At the bottom of the file, type in the following line:

VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.5

Save and exit nano. Now run .profile by typing:

source ~/.profile

Now install virtualenv.

sudo pip3 install virtualenv

Now install virtualenvwrapper.

sudo pip3 install virtualenvwrapper

We need to edit the .profile file again to set 1) the variable WORKON_HOME to the path of the directory .virtualenvs which contains our virtual environments and 2) make known the location of the shell file, virualenvwrapper.sh. In a terminal window type:

sudo nano ~/.profile

Type the following two lines at the bottom:

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

Save and exit nano. Run .profile by typing:

source ~/.profile

The command mkvirtualenv can now be used to create python virtual environments as shown below.

mkvirtualenv whatever -p /usr/bin/python3.5

VEs are stored in the hidden directory .virtualenvs located at your home path (~). The directory “whatever” is a subdirectory of .virtualenvs.

To activate the VE “whatever” in any Terminal window enter:

source ~/.profile
workon whatever

Your prompt now begins with (whatever). This means you are now working in the VE named “whatever”. All packages installed using pip3 (when the whatever VE is active) are placed in the site-packages directory located at:
~/.virtualenvs/whatever/lib/python3.5/site-packages.

To deactivate a virtual environment type:

deactivate

TinyML, AI, Edge Impulse, Arduino, Raspberry Pi, Pickleball

TinyML, AI, Edge Impulse, Arduino, Raspberry Pi, Pickleball