An introductory tutorial to python virtualenv and virtualenvwrapper

In this post lets talk about using Virtual Environment. This post will help you to get started with using virtualenv on windows and linux systems. It also introduces an awesome library virtualenvwrapper that makes life a lot easier while working with virtualenvs.

What is a virtual environment?

A virtual environment(developed by Ian Bicking) is a dedicated environment inside your computer, satisfying all the dependency of a particular project, with versioning kept in mind. It creates an isolated python environment where all the libraries that are required by an application are installed.

To understand the use case lets look at a small use case. We have a software Py27 written in Pyhton 2.7 and another software Py30 written in Python 3.0, inside an environment env27 we can easily run Py27 and we can create another environment env30 to run the Py30 software. Both the environments can reside in the same machine without creating any conflicts.

Why should we include it in our development stack?

Its a great way to test different version of the same package. Say i am a web developer currently all my projects are using Django 1.2. But here it is the new version of django released django 1.3, i do want to try my hands on it. Without affecting my global site_packages directory, i create a virtual environment for a test project and try django 1.3. The use cases can further be extended to testing, as to version compliance with dependent libraries.

How to get started with Virtual Environment?

Installation:
  1. On Windows:

    Install Python

    Install Distribute

    Install virtualenv : easy_install virtualenv

  2. On Linux(ubuntu):

    Install easy_install: sudo apt-get install python-setuptools python-dev build-essential

    Install pip: easy_install -U pip (PIP Installs Python Packages, developed by Ian Bicking)

    Install virtualenv : sudo easy_install virtualenv

Basic Usage:

to create a virtualenv: virtualenv <project_name>

to activate the environment: cd into the directory you can see three folders have been created bin, include and lib folder. and use the activate command source bin/activate

Introducing virtualenvwrapper

We can get started with virtualenv, but Doug Hellmann has created some real good bash script that reduces amount of time spent and organizes the work flow. Once done all our projects can be managed from the single place, like above to activate an environment we will not have to move to each of directories. So we shall install that too.

for windows:

run the following shell script in windows power shell.

for linux: sudo easy_install virtualenvwrapper

Open your bash script ~/.bashrc and add the following two lines:

export WORKON_HOME="$HOME/.virtualenvs" source $HOME/bin/virtualenvwrapper_bashrc close the bashrc file and source them: source ~/.bashrc

We are now ready to use the wrappers and hooks provided to us by the virtualenvwrapper:

Basic virtuanenvwrapper usage:

to make a virtual environment say test_project: mkvirtualenv test_project

to activate or work on that test_project: workon test_project

to deactivate the virtualenv: deactivate

Summary of the wrappers with functionalities:

  1. mkvirtualenv (creates a new virtualenv)

  2. rmvirtualenv (removes an existing virtualenv)

  3. workon (change the current virtualenv)

  4. add2virtualenv (add external packages in a .pth file to current virtualenv)

  5. cdsitepackages (cd into the site-packages directory of current virtualenv)

  6. cdvirtualenv (cd into the root of the current virtualenv)

  7. deactivate (deactivate virtualenv, which calls several hooks)

Using hooks:

Hooks are text files that are written in bin folder of the environment. They basically serve as activation and deactivation handlers. Say we want to login to the project root folder automatically once we activate the environment, we can create a postactivation hook.

Demo: Say we have an environment within it there will be three folders bin, include and lib however our source for the application we developed is inside the apps folder. We would ideally want to be inside the apps folder. We can easily do this by making a file called postactivate in the bin folder of the virtual environment and placing the following snippets: cd ~/test_app/apps after we activate next time we shall be auto taken to the directory.

Summary of hooks:

  1. postmkvirtualenv (specify an action after a virtual env is created)

  2. prermvirtualenv (specify an action before a virtual env is removed)

  3. postrmvirtualenv (specify an action after a virtual env is removed)

  4. postactivate (specify an action after a virtual env is activated)

  5. predeactivate (specify an action before a virtual env is deactivated)

  6. postdeactivate (specify an action after a virtual env is deactivated)

Using Pip and Requirements file:

Lastly we have this handy tool called pip, which takes care of the libraries, softwares and their versions that have been installed. To prepare a list of dependencies of the apps we use pip freeze > requirements.txt to install the requirements of any other project we use pip install -r requirements.txt

Further Hacking and Further Reading. Hope the post helps.

Comments

blog comments powered by Disqus