Skip to content

Create Virtual Environment (Linux)

Jorge Macias edited this page Dec 25, 2016 · 6 revisions

Fish Bowl

What is virtualenv?

virtualenv is a tool that allows you to create isolated Python environments, which can be quite helpful when you have different projects with differing requirements. As this is the case with TurboGears, it can be indispensable when working on those.

What is virtualenvwrapper?

virtualenvwrapper is just that, a wrapper utility around virtualenv that makes it even easier to work with. I admit that I have never used virtualenv without virtualenvwrapper, and I do not intend to. For that reason this post will only cover working with virtualenv via virtualenvwrapper. Note also that virtualenvwrapper is a set of shell functions that are guaranteed to work in the following shells:

Installation

Both virtualenv and virtualenvwrapper can be installed via pip. Install virtualenv first and then virtualenvwrapper. Use the following commands to install them:

sudo pip install virtualenv
sudo pip install virtualenvwrapper

Configuration

In order to use virtualenvwrapper you should add two lines to your shell startup .bashrc file at ~ directory.

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

The first line tells virtualenvwrapper where to store the virtualenvs that will be created and used. The example above stores them in a folder called .virtualenvs inside your home folder. The first line runs the shell script to set up the virtualenvwrapper commands and should point to the location where virtualenvwrapper was installed. There are a lot of commands available with virtualenvwrapper, all of which are well documented. In my experience, the following are the commands you will use most often:

mkvirtualenv - used to create a new virtual environment. When you create a new environment it automatically becomes the active environment.
rmvirtualenv - used to remove an existing virtual environment. The environment must be deactivated (see below) before it can be removed.
workon - used to activate a virtual environment. Will also list all existing virtual environments if no argument is passed.
deactivate - used to deactivate the currently active virtual environment. Note that workon will automatically deactivate the current environment before activating a new one.

Note: In order to start using the wrapper you must exit your terminal session and open again to gain access to the commands that i explain before.

Clone this wiki locally