Setting up Virtual Development Environments for Python

Setting up virtual environments for Python is always a great way to keep your projects and their related Python packages independent.


Virtualenv is the de-facto standard choice for Python environments. It’s capable of creating independent Python module groupings which you can switch to and from using commands. Unfortunately, virtualenv does not install and manage multiple versions of Python, but we’ll get to a solution for that with pythonbrew later.

To install virtualenv, simply use pip:

sudo pip install virtualenv

If you would like bash completion, you can use the script created by Eugene Kalinin:

git clone git://
sudo cp ./virtualenv-bash-completion/virtualenv /etc/bash_completion.d/
source ~/bashrc

You may now use virtualenv and install packages within environments as either root or an ordinary user on the system.

Create an environment as follows:

virtualenv python-env

You now have a local instance of Python which can be found at ./python-env/bin/python

You may then go ahead and install packages in the current directory by using the virtualenv’s pip executable.

./python-env/bin/pip install Flask

All packages will placed in the local virtualenv under ./python-env/lib/python2.7/site-packages/

Rather than having to type such nasty pathnames to get to pip and python, virtualenv has a cool little activate script which will alter your entire working environment so that you can use all tools as you would if they were installed on the system.

source ./python-env/bin/activate

You may now use the tools as per usual:

(python-env)fots@fotsies-ubprecise-01:~$ which python
(python-env)fots@fotsies-ubprecise-01:~$ which pip

To return to your normal environment, simply type deactivate:


I suggest adding the following to the end of your .bashrc to help simplify activation of virtualenvs:

# Switch to a Python virtual environment
    source "$1/bin/activate"

You may now type the following to activate a virtual environment for Python:

activate ./python-env

Pythonbrew Installation & Usage

Pythonbrew takes virtualenv a lot further, allowing for installation of various Python versions and management more akin to the amazing RVM for Ruby.

Install pythonbrew as follows:

curl -kL | bash
echo "[[ -s $HOME/.pythonbrew/etc/bashrc ]] && source $HOME/.pythonbrew/etc/bashrc" >> ~/.bashrc
source ~/.bashrc

Since pythonbrew will build Python from source, we need to be prepared:

sudo apt-get install build-essential zlib1g-dev

Installing Python versions may be done as follows:

pybrew install 2.7.3

It’s a good idea to switch on verbose mode if you want to see what’s going on:

pybrew install --verbose 2.7.2

To view the newest Python version for each sub-version and get an idea what you can install:

pybrew list -k

Permanently switch to a Python version as follows:

pybrew switch 2.7.2

Or temporarily switch to a Python version in the current session:

pybrew use 2.7.3

Jump back to the system’s Python version by using:

pybrew off

You may also view all installed Python versions:

pybrew list

Uninstall a version of Python as follows:

pybrew uninstall 2.7.2

To upgrade pythonbrew to the newest version:

pybrew update

You may run your Python script against all installed versions of Python using:

pybrew py -v

Pythonbrew Environments Using Virtualenv

Pythonbrew can now use virtualenv to create environments. Install virtualenv for pythonbrew as follows:

pybrew venv init

Creating a project:

pybrew venv create proj

Much like RVM, projects are related to each version of Python that’s installed. Thus, you may have a project called “hello” twice, one against version 2.7.3 and one against 3.2.3 if you like.

List your projects

pybrew venv list

To switch to a project, type:

pybrew venv use proj

To de-activate the environment:


To delete an environment:

pybrew venv delete proj

You may install packages using pip which is available independently for each environment automatically.

Pythonbrew Shortcomings

Pythonbrew has so much potential and could easily be as good as RVM, but there are a few problems:

If I have some spare time one day, I’ll attempt to tackle these problems so that us Python-heads can have all bells and whistles in this otherwise excellent application.

comments powered by Disqus
comments powered by Disqus