I've been busy the last couple of weeks. Recently, I started a late summer/early autumn internship at ScraperWiki, who are building a data science platform. I should have a blog post (it's drafted!) describing how I've found working there so far posted soon on their blog.

Anyway, I've been pairing with the very helpful and patient David Jones who has been helping me out hugely by sharing some of his knowledge with me. For the last two days, I've been learning a lot, and somehow forgetting even more, about vim. However, David also showed me how to use virtualenv today. This was far easier for me to get my head round, so I'd figured I'd write up a quick start guide while it was fresh in mind.

What is virtualenv?

As the homepage describes it, "virtualenv is a tool to create isolated Python environments". These environments are also referred to as virtualenvs.

Why is this useful for software development?

It greatly simplifies dependencies on other modules for different software projects you are working on. Each virtualenv can have its own installed modules. If projects require different versions of the same module, it would be a real pain to manage this at a system level. You might need, say, two different versions of the same module for two different projects. Using virtualenv, it's simple for every project you work on to have its own set of modules independently of any others.

You can also use this to safely test new versions of modules with your software: make a new virtualenv and install the newer version of the module there.

Furthermore, you can also setup virtualenvs that use different Python versions, particularly handy considering that the change from Python 2 to Python 3 is a substantial one.

Another plus is that using virtualenvs stops you polluting your system's actual Python installation with lots of packages that you're using only to develop certain software.

How do you install it?

On Debian-based distributions, e.g. Ubuntu, you can use apt-get: sudo apt-get install python-virtualenv

How do you use it?

In a terminal, make a directory for the project you're working on (or enter an existing directory), e.g. newproject. Creating a new virtualenv is as easy as entering virtualenv newproject

When this command is entered, a Python distribution will be setup within the specified directory, in this case newproject. The current default behaviour of virtualenv is not to copy any system installed packages here, so it's like a fresh install.

To activate a virtualenv, you then change to your project directory (e.g. cd newproject), then enter the command: . bin/activate (notice the space between the dot and bin/activate).

It's easy to tell you're running a virtualenv: the terminal will show (newproject) before the command prompt. While the virtualenv is active, any pip install packagename command will install a Python package into that particular virtualenv. This is another nice benefit of virtualenv: you don't need to run *sudo* pip install packagename as you will already be working in a user-writeable directory.

To deactivate a virtualenv, simply enter deactivate at the terminal. To use in future, just run the activate command again in the project directory: . bin/activate

Simple, but incredibly useful.

(Bonus note: Since creating a virtualenv creates directories such as /bin, /include, /lib and /local, you may wish to add these to a .gitignore file if you're using git so that you don't accidentally commit these files to a repository.)

(Bonus note 2: After initially posting this, it was later pointed out to me that virtualenvwrapper is a handy way to manage virtualenvs.)