Derek Kwok's Blog

Blog about Software Development, Techniques and Discoveries.

Using virtualenv, virtualenvwrapper for your Python projects

| 0 comments

This post will not cover why you should use virtualenv/virtualenvwrapper for working on Python projects. For that, you can find an excellent presentation here: http://mathematism.com/2009/07/30/presentation-pip-and-virtualenv/

Checking if pip is installed

Make sure you have pip installed. You can check if you have it installed by typing pip into your terminal:

checking if pip is installed

If you don’t have pip installed but you do have easy_install, you can install pip this way:

Via easy_install:

$ sudo easy_install pip

Other methods of installation for pip can be found at: http://www.pip-installer.org/en/latest/installing.html

Installing virtualenv and virtualenvwrapper

On Ubuntu machines, you might need to run the following before you can install virtualenv and virtualenvwrapper properly:

$ sudo apt-get install python-setuptools python-dev build-essential

I was able to install it on my Mac without any issues (although I already have XCode installed).

To install virtualenv and virtualenvwrapper:

$ pip install virtualenv
$ pip install virtualenvwrapper
...
$ export WORKON_HOME=~/Envs
$ mkdir -p $WORKON_HOME
$ source /usr/local/bin/virtualenvwrapper.sh
$ mkvirtualenv env1
Installing
distribute..........................................
....................................................
....................................................
...............................done.
virtualenvwrapper.user_scripts Creating /Users/derekkwok/Envs/env1/bin/predeactivate
virtualenvwrapper.user_scripts Creating /Users/derekkwok/Envs/env1/bin/postdeactivate
virtualenvwrapper.user_scripts Creating /Users/derekkwok/Envs/env1/bin/preactivate
virtualenvwrapper.user_scripts Creating /Users/derekkwok/Envs/env1/bin/postactivate  New python executable in env1/bin/python
(env1)$ ls $WORKON_HOME

You may also want to add these 3 lines to your shell startup file (.bashrc, .bash_profile, .profile, etc…):

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh

You can find the full setup documentation of virtualenvwrapper at: http://www.doughellmann.com/docs/virtualenvwrapper/

Setting up your Virtual Environment for work with your Python Project

In the example below, we’re working on a Django project. You might consider creating a new virtualenv for your work:

$ mkvirtualenv myvirtualenv
(myvirtualenv) $ pip install Django==1.3.1
(myvirtualenv) $ pip install south
...
(myvirtualenv) $ python manage.py runserver

When you open up a new terminal window, your virtual environment is automatically activated. To re-activate your virtual environment, you can use the following commands:

$ workon myvirtualenv
(myvirtualenv) $ 

This command will automatically put you into “myvirtualenv” environment.

Leave any questions or comments below!

Leave a Reply

Required fields are marked *.

*