PlayNicely - Beautiful collaboration for software developers

Python library management with Pip, Virtualenv

Posted by adam



PlayNice.ly has grown greatly in the many months since its inception. What started as a small Python project on a local virtual machine is now deployed across several environments and servers. This progression has called for a practical way of managing our third-party libraries. We have found Pip and Virtualenv invaluable for this purpose, and we wanted to share our experiences.

Developers: You can signup for our private beta or register for the Redis London meetup



h2. The reasoning

Most Python projects require other Python libraries in order to work. A web app may require Django, or an image processing app may require PIL. Having these libraries is fantastic, they save you a lot of work and are generally very reliable. So how do you install them? Well, simple:

# The basic way of installing packages
easy_install django

Note: You may need to install the python-setuptools on your Ubuntu/CentOS box

That command will happily trot off and install the latest version of Django on your system. Great!

But what do you do if you are working with legacy code and you need an older version of Django? Or, and here is the catch, what if one app on the server needs Django 0.9, and another app needs Django 1.1? Or what if you want to upgrade a package for one app, without affecting the rest of the server. Well, you are out of luck. :(

Enter Pip and Virtualenv. Pip is a replacement for the (outmoded) easy_install command. Virtualenv allows you to created Python environments isolated from the rest of the system.

Before we move on, let’s get Pip and Virtualenv installed:

# Install pip using apt-get
sudo apt-get install python-pip
 
# Install virtualenv and distribute using pip
sudo pip install virtualenv distribute

Virtualenv

First, let’s setup our virtual environment. Move to your project’s root directory, then run the following:

# Create our virtual environment ignoring any globally
# installed packages. This will create the env_dev directory
# within the current directory.
virtualenv --no-site-packages env_dev
 
# Setup the shell session for our new virtual environment
. env_dev/bin/activate
 
# Optional: I also like to have my environment activated when I login as
# it saves always having to repeat the above line:
echo ". env_dev/bin/activate" >> ~/.bashrc

Pip

Now that we have our project’s Python environment setup, we need to populate it with some packages (using Pip). First, we need to tell Pip to use our virtual environment, rather than the server’s global environment:

# Tell pip to require, and always use, the available virtual
# environment (otherwise it will install packages globally)
export PIP_REQUIRE_VIRTUALENV=true
echo "export PIP_REQUIRE_VIRTUALENV=true" >> ~/.bashrc
export PIP_RESPECT_VIRTUALENV=true
echo "export PIP_RESPECT_VIRTUALENV=true" >> ~/.bashrc

It is a good idea to keep a list of required packages. Pip can use this to setup/update your virtual environment. We will put this in a file called requirements-dev.pip:

django
python-cjson
pytz
pyres
textile
-e git+git://github.com/andymccurdy/redis-py#egg=redis

Of course, you should modify the above list to be applicable to your own project. Once you have done so, you can install the packages as follows:

pip install -r requirements-dev.pip

And there you have it, all is installed! If in doubt, just fire up a Python prompt and try to import a package:

# Check Django was installed ok
import django

Resources

Private beta signup

PlayNice.ly will be in private beta soon. Signup now and be first in line.



2 Responses to “Python library management with Pip, Virtualenv”

  1. [...] This is a pretty basic way of installing packages. See our blog post on Pip & Virtualenv for something more [...]

  2. ismail says:

    Just saw this… in any case virtualenv lets you create a bootstrap script, that can automatically install packages with pip.

    so you run one command

    bootstrap.py

    Been experimenting a bit with it, check simple-bootstrap on Github

Leave a Reply