26th June 2015 | By:

Python Virtual Environment Tutorial

This is a basic tutorial designed to help you setup a virtualenv and start using it.

The purpose of virtualenv is to isolate Python environments and make sure there are no conflicts when you have multiple applications deployed on the same machine. In short if you have two or more Python applications which require different versions of the same package can be nicely isolated by using virtualenv.

The installation process is straightforward:

pip install virtualenv

After the installation you will have a virtualenv command available, you can now start creating your first virtual environment.

virtualenv venv_foo

This will create a folder venv_foo which will be your isolated environment. You can create as many environements as you wish. Now let’s activate the environment and start installing some packages within that environment.

. venv_foo/bin/activate
pip install python-dateutil

Once you have activated the virtual environment your command line will indicate which virtual environment you are in. Everything you install within that environment will only be available when you are within that environment. You can list the installed packages within that environment by using the pip freeze.

pip freeze

It is also possible to run stand alone python scripts which make use of a specific virtual environment.

import os
import sys

# setup virtual env
if == 'posix':
	bin_name = 'bin'
	# be nice to win32
	bin_name = 'Scripts'

activate_this = '/path/to/your/venv/%s/' % bin_name
execfile(activate_this, dict(__file__=activate_this))

Once you are done and you want to quit the environment you can use the following command:


Deleteing a virtual environment is as simple as deleting the folder.


Tags: , , ,

1 Comment

Leave a reply

Your email address will not be published. Not now, not ever. Required fields are marked *


You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>