virtualenv - shawfdong/hyades GitHub Wiki

virtualenv is tool to create isolated Python environments. It creates an environment that has its own installation directories, that doesn’t share libraries with other virtualenv environments. virtualenv addresses the problem of dependencies and versions for Python modules.

For example, I recently installed QSTK (QuantSoftware ToolKit, a Python-based framework designed to support portfolio construction and management) on my Mid 2010 27-inch iMac and I used virtualenv to solve the problem of version conflict. QSTK depends on an older version of the datetime module (python-dateutil 1.5 to be exact). However, the globally installed datetime module is much newer at version 2.4.0. virtualenv to the rescue!

The primary Python I use on my iMac is the python27 distribution provided by MacPorts. Let's install virtualenv first:

$ sudo port install py27-virtualenv

$ sudo port select --set virtualenv virtualenv27
Selecting 'virtualenv27' for 'virtualenv' succeeded. 'virtualenv27' is now active.

$ which virtualenv
/opt/local/bin/virtualenv

Create a directory for virtualenv environments in the home folder:

$ mkdir ~/py27-virtualenv

Create a virtualenv environment for QSTK:

$ cd ~/py27-virtualenv
$ virtualenv QSTK

Activate the virtualenv environment:

$ source ~/py27-virtualenv/QSTK/bin/activate
The prompt will change, e.g., from dong@manjusri: ~/py27-virtualenv $ to (QSTK)dong@manjusri: ~/py27-virtualenv $ .

Install QSTK:

$ pip install QSTK
which will also install its dependencies in the same virtual environment:
$ pip list
matplotlib (1.4.3)
mock (1.0.1)
nose (1.3.4)
numpy (1.9.1)
pandas (0.15.2)
pip (1.5.6)
pyparsing (2.0.3)
python-dateutil (1.5)
pytz (2014.10)
QSTK (0.2.8)
scikit-learn (0.15.2)
scipy (0.15.1)
setuptools (3.6)
six (1.9.0)
wsgiref (0.1.2)

When done working in the virtual environment for the moment, we can deactivate it:

$ deactivate
⚠️ **GitHub.com Fallback** ⚠️