1---2title: 'Python - virtualenv is your friend'3date: '2012-05-25'4published_at: '2012-05-25T17:25:00.000+10:00'5tags: ['development', 'pip', 'programming', 'python', 'sysadmin', 'virtualev']6author: 'Gavin Jackson'7excerpt: 'My manager (www.nathanr.net) recently shared the following technique that we use to manage our python instances, I thought it was so good, I wanted to share it with you. No longer stress about what ve...'8updated_at: '2012-06-18T12:03:14.113+10:00'9legacy_url: 'http://www.gavinj.net/2012/05/python-virtualenv-is-your-friend.html'10---1112[](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicCX1zBSSPc1RezUh3I15Tq9aCJWJJMZwWzBcQaRfLK4C_qLIXLcLq9mpQZ0cF-xSAwBTZJaJzjL1X60swoH4UARzndR6Ee7IQYoPnu3BIf2rFFiMp9xqwBUR1kubcT8dWqRa56WVXZHA/s1600/woma-python.jpg)1314My manager (www.nathanr.net) recently shared the following technique that we use to manage our python instances, I thought it was so good, I wanted to share it with you.1516No longer stress about what version of python is installed with your Linux distribution.1718Python source + virtualenv + pip is your savior.1920**Virtualenv** ([http://pypi.python.org/pypi/virtualenv](http://pypi.python.org/pypi/virtualenv)) is a bit like a vm for a python install, it takes a vanilla python build (typically from source) and allows you to "activate" it (replacing your default python binaries and libraries with the ones within the virtualenv instance.2122This has the benefit of freeing you from the outdated python binaries found in most Linux distributions, and allowing you to tailor specific library configurations for each environment.2324**Pip** ([http://pypi.python.org/pypi/pip/1.1](http://pypi.python.org/pypi/pip/1.1)) is a tool for installing and managing python packages (a bit like CPAN for python). We can use this within the virtualenv instance to manage our installed libraries.2526By default it queries the online python package index (PyPI - [http://pypi.python.org/pypi](http://pypi.python.org/pypi)).2728## Building python and installing virtualenv into it2930Before running the following, you will need to download the python source code (http://www.python.org/download/) and virtualenv (http://pypi.python.org/pypi/virtualenv).3132**#install dev libraries (SLES SP2)** **#SUSE build tool pattern (same as build-essential on Ubuntu) ** zypper install zypper install -t pattern Basis-Devel zypper install libbz2-devel zypper install readline-devel zypper install ncurses-devel zypper install libopenssl-devel zypper install libxslt-devel3334**#build python**3536tar -xjf Python-2.7.3.tar.bz23738cd Python-2.7.3/3940./configure --prefix=/usr/local/python-2.7.34142make4344make install4546ln -s python-2.7.3 python-2.74748ln -s python-2.7 python4950**#install virtualenv ** tar -xzf ~/Downloads/virtualenv-1.7.1.2.tar.gz cd virtualenv-1.7.1.2/5152/usr/local/python/bin/python setup.py install5354Ok, we now have a Python 2.7 install with virtualenv in it. Now, create a virtualenv for our development and make it your working virtual environment:5556**#set up virtualenv in environment-name** /usr/local/python/bin/virtualenv environment-name New python executable in environment-name/bin/python Installing setuptools............done. Installing pip...............done.5758**#activate the virtual environment ** cd environment-name/. bin/activate (environment-name)user@host:~/tmp/environment-name>5960## Installing python libraries into the virtualenv instance6162Note that some libraries in the online pip repositories are somewhat dated, to ensure that we have the correct versions we maintain a local cache (which we keep checked into our mercurial repository).6364**pip install --requirement=~/work/libraries/python/python-pip-freeze.txt --find-links=file://$HOME/work/libraries/python/cache --no-index**6566Where the cache folder contains the tar.gz files for all of the libraries contained in the python-pip-freeze.txt file (this file can easily be regenerated using the **pip freeze** command).6768In our case the file contains the following libs:6970**CherryPy==3.2.2 Mako==0.7.0 MarkupSafe==0.15 PyXB-base==1.1.3 SQLAlchemy==0.7.7 Twisted==12.0.0 psycopg2==2.4.5 pyOpenSSL==0.13 reportlab==2.5 suds==0.4 wsgiref==0.1.2 zope.interface==3.8.0 pyclamdplus==1.0b2 python-magic==0.4.2 six==1.1.0 python-dateutil==2.1 lxml==2.3.4 python-daemon==1.5.5**7172## Pulling it all together7374We use the following helper script to create a new virtualenv environment and deploy our required libraries into this environment:7576**#!/bin/bash7778# Need two parameters:79# 1. Which installed python to use80# 2. Where the virtualenv should be81if [[ $#!= 2 ]]; then echo "Usage: build-python-env.sh " echo "eg. For the distribution shipped python, you might do: \"build-python-env.sh /usr pyenv-test\"" exit 1 fi ZKDIR=`dirname $(readlink -f $0)` echo $ZKDIR $1/bin/virtualenv $2. $2/bin/activate pip install --requirement=$ZKDIR/libraries/python/python-pip-freeze.txt --find-links=file://$ZKDIR/libraries/python/cache --no-index ** ** ** ** **828384