View on GitHub

Introduction to Raspberry Pi

Artisan's Asylum

Python Virtual Environments (virtualenv)

  • Testing different versions of libraries (one API might require python-requests v3 another might need v2)

Installing virtualenv

sudo pip install virtualenv

Using your first virtualenv

To create your first virtualenv, run:

virtualenv flaskenv

This will create a virtual environment without any of the packages installed on your Raspberry Pi. If you want to keep the packages already installed, add the "--system-site-packages" option to the virtualenv command.

To begin using the virtualenv, change into the new directory and run:

pi@ericpi ~/flaskenv$ cd flaskenv
pi@ericpi ~/flaskenv$ source bin/activate
(flaskenv)pi@ericpi ~/flaskenv $

Notice that the command prompt changes, once you enter the venv so you know where you are. The (flaskenv) will disappear once you exit the virtualenv later.

At this point, any python libraries you install will only be installed in that virtual environment and will not affect the rest of the system. To see what packages you have installed, run:

# If installing yolk outside the venv, prefix the command with "sudo"
easy_install yolk
yolk -l

On my system, there are only 5 packages present in flaskenv, while in a virtualenv created with --system-site-packages there are a bit over 10. Remember that installing yolk only affects the current virtualenv, so to run it again on your base system, you will need to do easy_install again.

To use the virtualenv in a python script, change the #!/usr/bin/python at the top of the file to #!/bin/python

To get out of the virtualenv, run "deactivate" at the bash prompt.