Skip to content

Python virtual environment

utunga edited this page Jan 10, 2012 · 8 revisions

Lets use pip and virtualenv

Prerequisites:

    sudo easy_install virtualenv
    sudo pip install virtualenvwrapper

put this in your bashrc:

    #Virtaulenvwrapper, pip
    export WORKON_HOME=~/.virtualenv
    mkdir -p $WORKON_HOME
    source /usr/local/bin/virtualenvwrapper.sh
    export PIP_DOWNLOAD_CACHE=~/.pip/downloads
    mkdir -p $PIP_DOWNLOAD_CACHE
    export PIP_RESPECT_VIRTUALENV=true
    export PIP_VIRTUALENV_BASE=$WORKON_HOME
    eval "`pip completion --bash`"
    export PIP_USE_MIRRORS=true

Skim these:

http://www.clemesha.org/blog/modern-python-hacker-tools-virtualenv-fabric-pip

http://www.doughellmann.com/projects/virtualenvwrapper/

Get going:

    mkvirtualenv hashmapd
    cd your_hashmapd_directory
    git pull
    pip install -e .

From now on, when you are using this environment, you will be able to go import hashmapd in any python file. And the nice thing is that there will be no name collisions with system installations or with other virtual environments. When you are done with hashmapd, type

    deactivate

and it will drop you back into your regular environment. To start working on hashmapd again go

   workon hashmapd

##A tighter sandbox

if you put the line

export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'

in your bashrc before you make the virtual environment, then it will be completely corralled off without access to the system python packages. This is nice, as it gives you complete control. A catch is that you need to have the necessary development header files for matplotlib to compile. On ubuntu, you need to run sudo apt-get build-dep python-matplotlib This installs a gazillion -dev packages you probably haven't needed before, taking up 370MB of space. Ouch. Once you have done this, then run the additional command:

   pip install -r requirements.txt

Pip will download stuff and build it. Because scipy and matplotlib, in particular, are kind of complicated, this will take a while. However, we will all be using the same versions of our python packages.

Clone this wiki locally