Beginners Guide : pip and virtualenv

Up until recently I have been maintaining Python dependencies manually in my development environment. I would do things like editing the python path and symlinking in python packages, which made me feel very clever until, it quickly turned into the 7th level hell. I would quickly forget where I put things and waste a bunch of time trying to figure out why things were breaking all the time. Don’t even mention trying to figure out what I needed to export when trying to deploy or reproduce the environment for a collaborator.

Now on to virtualenv!


I will be basing this tutorial on the setup I have at home. So for the best results using this guide I would recommend using Ubuntu 10.10. I will try and keep the instruction as generic as possible. Overall goal will be to introduce you to the magic of pip and virtualenv.

Install easy_install

Now it may seem unintuitive to install a tool with the very tool its trying to out date, but it easiest way I have found to do it.

You will need to install easy_install. Once that is installed you are ready to get `pip`

Since were installing things you might as well install virtualenv.

Creating a virtual environment

The problem that virtualenv is trying to solve is that when you install multiple python package often with various installation methods.  It will become increasingly more difficult to keep your core system in order. The idea with virtualenv is to create disposable and quickly reproducible environments to run your python projects.

For ease on my system I have mapped /srv/virtual/ the space where I keep all my virtualenv. So lets say I have Django project I am working on I would name the virtualenv something along the lines of django-<projectname>.

The –no-site-packages option tells virtualenv not to inherit any libraries from your existing Python installation’s site-packages. To activate the newly created virtualenv, you will need to do the following.

Once you have activated the virtualenv you are running within a system that has all the packages you have installed to that particular virtualenv. You can freely browse your file system and run tools as normal; but with the added value that you have the newly installed packages in separated and clean environment maintained by you.

So know we have just created a pristine environment to begin customizing for our projects needs. Enter pip. Pip is designed to make sense of all the various ways you can install python packages on your system. It's trying to solve the problem of how to consistently add and remove desired python packages without any adverse affect on system. I cant tell you how many times I have forgotten the install method for some package and I have to go rummaging through my python path to attempt to remove all the installed eggs. Ugh not fun.

We can use pip to directly install into the virtualenv just by specifying it. In order to do the following your working directory will be the directory that you just created your virtualenv within. In this case it will be the parent directory of the django-mimo virtualenv

Now this package wont be accessible outside of the virtualenv, which is exactly the desired outcome.

Now lets say you have a friend and he wants a copy of your pristine django virtualenv with all the correct package for a project you guys are working on. Do the following.

Thats gives him everything you have installed and he can then turn around and create his own virtualenv with the same exact setup.

Everything just works.

Mahdi Yusuf

engineer, speaker, writer, and home labber.