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?
Install virtualenv :
sudo apt-get install python-setuptools python-dev build-essential
easy_install -U pip(PIP Installs Python Packages, developed by Ian Bicking)
Install virtualenv :
sudo easy_install virtualenv
to create a virtualenv:
to activate the environment: cd into the
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.
run the following shell script in windows power shell.
sudo easy_install virtualenvwrapper
Open your bash script ~/.bashrc and add the following two lines:
close the bashrc file and source them:
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:
to activate or work on that test_project:
to deactivate the virtualenv:
Summary of the wrappers with functionalities:
mkvirtualenv (creates a new virtualenv)
rmvirtualenv (removes an existing virtualenv)
workon (change the current virtualenv)
add2virtualenv (add external packages in a .pth file to current virtualenv)
cdsitepackages (cd into the site-packages directory of current virtualenv)
cdvirtualenv (cd into the root of the current virtualenv)
deactivate (deactivate virtualenv, which calls several 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.
Say we have an environment
Summary of hooks:
postmkvirtualenv (specify an action after a virtual env is created)
prermvirtualenv (specify an action before a virtual env is removed)
postrmvirtualenv (specify an action after a virtual env is removed)
postactivate (specify an action after a virtual env is activated)
predeactivate (specify an action before a virtual env is deactivated)
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