Essential Python Tools: virtualenv and pip

A wise man once said ‘Necessity is the mother of invention’. But I think the real mother of invention is laziness. If you are lazy enough, you will find an easier way to do a particular thing. In order to solve large problems elegantly, we need to use tools that solve smaller sub-problems very well. To the coder in you, yes, it’s a bit like dynamic programming! If you have fiddled with Python, you would have definitely come across many different libraries and packages. Sometimes, you wish that you had different machines for different libraries because the requirements differ from one thing to another. There should be an easier way to manage different environments and packages right?  


virtualenv is an amazing tool that lets you create isolated Python environments. Now why would we wanna do that? Well, a lot of times, we want to customize the environment for a particular project. But we don’t want these changes to affect our other projects. In fact, most of the times, such changes will break other builds. Hence we need to isolate this environment. It is really easy to install and simple to use. Here’s how you install it for *nix style operating systems:

Open up the terminal and type the following:

$ easy_install virtualenv
$ virtualenv myenv
$ cd myenv
$ source bin/activate

And there we go, all done! You can change anything you want inside this virtual environment and it will not affect any of your other projects. We will talk later about what easy_install is why you shouldn’t use it anymore. For now, let’s see where it is actually used. This tool is very useful when you are developing an application and you want to upgrade the framework. If you are afraid that upgrading the framework will break your app, then you can create a new environment and test it out. virtualenv is designed to address issues which cannot be solved using apt-get. This tool is indispensable and if you use Python enough, you will most certainly end up needing it.


Here’s the thing about Python: it comes in packages. It’s not the same as a distribution, although usually a distribution will have the same name as a package. Anyway, a Python package is something you import. There are so many packages and so many things to take care of that there are tools called package managers to do this. easy_install is one such tool. The basic functionality it provides is that given a name, it can search for a package with that distribution name, and also satisfying a version requirement. It then downloads the package and installs it (using install). After that, it checks the newly installed distribution to see if it requires any other libraries that aren’t yet installed, and if so it installs them. Why do we need pip? pip does everything easy_install does, but in a much better way.

Unlike easy_install, it downloads all distributions up-front, and generates the metadata to read distribution and version requirements. There are a few other advantages as well, but I will not go into further details. Just note that pip does the job better than easy_install by fixing a few smaller issues. pip can be easily installed from git, mercurial or svn.

Another great thing about pip is that it works really well with virtualenv. We create an isolated Python environment using virtualenv and install all the necessary packages using pip. Works like magic!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s