-
Notifications
You must be signed in to change notification settings - Fork 0
Create Virtual Environment (Linux)

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.
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:
Both virtualenv and virtualenvwrapper can be installed via pip. Install virtualenv first and then virtualenvwrapper. Use the following commands to install them:
Python 2
sudo pip install virtualenv
sudo pip install virtualenvwrapper
Python 3
sudo apt-get install python3-pip
sudo pip3 install virtualenv
sudo pip3 install virtualenvwrapper
In order to use virtualenvwrapper you should add two lines to your shell startup .bashrc file at ~ directory.
Python 2
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
Python 3
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
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.
[Back]