Virtualenv

What is Virtualenv?

When working with many projects, sooner or later there will be conflicts between the used package versions.

For instance an old project might depend on Django 1.2, and can’t be migrated because lack of time. At the same time, a new Project with Django 1.4.1 has to be started. Such problems can be solved with virtualenv.

virtualenv creates a “container” for each project, containing the installed packages and separates them from the system version.

In addition, virtualenv can assign different Python versions to different environments.

virtualenv is also suitable for the production environment on the live server.

Installation

virtualenv can be installed with pip:

$ pip install virtualenv

Note

Root privileges may be required on Linux or Mac OS X.

After the installation, a folder for all virtual environments should be created. For instance in the home directory.

$ mkdir .virtualenvs

Simplification through Virtualenvwrapper

To simplify working with virtualenv on Linux or Mac OS X, you can install the package virtualenvwrapper:

$ pip install virtualenvwrapper

Add the following lines to the file .bashrc or .profile. In your home directory.

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

This lets virtualenvwrapper know, where the virtual environments are located. The script virtualenvwrapper.sh loads the shell commands we will work with.

After editing .bashrc or .profile, the configuration has to be loaded manually so virtualenvwrapper can create the necessary scripts.

$ source .bashrc
virtualenvwrapper.user_scripts creating /home/vagrant/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /home/vagrant/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/vagrant/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/vagrant/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/vagrant/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/vagrant/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /home/vagrant/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /home/vagrant/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /home/vagrant/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /home/vagrant/.virtualenvs/get_env_details
virtualenvwrapper.user_scripts creating /home/vagrant/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /home/vagrant/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /home/vagrant/.virtualenvs/prermproject
virtualenvwrapper.user_scripts creating /home/vagrant/.virtualenvs/postrmproject

Table Of Contents

Previous topic

Python

Next topic

Django

This Page