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

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:

1
sudo pip install virtualenv

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

1
2
3
git clone git://github.com/ekalinin/virtualenv-bash-completion.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:

1
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.

1
./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.

1
source ./python-env/bin/activate

You may now use the tools as per usual:

1
2
3
4
(python-env)fots@fotsies-ubprecise-01:~$ which python
/home/fots/python-env/bin/python
(python-env)fots@fotsies-ubprecise-01:~$ which pip
/home/fots/python-env/bin/pip

To return to your normal environment, simply type deactivate:

1
deactivate

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

1
2
3
4
5
# Switch to a Python virtual environment
activate()
{
    source "$1/bin/activate"
}

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

1
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:

1
2
3
curl -kL http://xrl.us/pythonbrewinstall | 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:

1
sudo apt-get install build-essential zlib1g-dev

Installing Python versions may be done as follows:

1
pybrew install 2.7.3

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

1
pybrew install --verbose 2.7.2

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

1
pybrew list -k

Permanently switch to a Python version as follows:

1
pybrew switch 2.7.2

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

1
pybrew use 2.7.3

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

1
pybrew off

You may also view all installed Python versions:

1
pybrew list

Uninstall a version of Python as follows:

1
pybrew uninstall 2.7.2

To upgrade pythonbrew to the newest version:

1
pybrew update

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

1
pybrew py -v test.py

Pythonbrew Environments Using Virtualenv

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

1
pybrew venv init

Creating a project:

1
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

1
pybrew venv list

To switch to a project, type:

1
pybrew venv use proj

To de-activate the environment:

1
deactivate

To delete an environment:

1
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