Introduction
Virtualenv is a tool that allows you to isolate your python application. It's very usefull when you have two application that conflicts on dependencies. Virtualenvs uses the PATH enviroment variable to switch python interpreter, so every time you need to know which virtualenv are you using try the command which python. When you have a default Ubuntu installation, which python usually return the system python interpreter /usr/bin/python. Also virtualenvs solves the problems of the requirement of root privileges, since it will install all dependencies on your user home directory instead of system directories.
Step 1: Requirements
Pip package manager is required:
sudo apt-get install python-pip python-dev build-essential
sudo pip install --upgrade pip
Step 2: Installation
If you have sudo permissions, you can install it on the system with:
sudo pip install virtualenv virtualenvwrapper
If you don't have permission try to install with --user option:
pip install --user virtualenv virtualenvwrapper
Now if you are using bash, open the .bashrc of your home directory to add:
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh
If you are using zsh, add the exports to the .zshrc.
Step 3: How to use it
Virtualenvwrapper helps to use and manage virtualenvs, it adds some helpful commands like:
- mkvirtualenv project_name: it will create an empty virtualenv called project_name.
- workon project_name: switch between or enters the project_name virtualenv.
- rmvirtualenv project_name: it will delete the virtualenv called project_name.
- lsvirtualenv: lists all created virtualenvs.
Let's see an example:
mkvirtual test_virtualenv_1
pip install requests
pip freeze | grep requests
the output should be similar to:
requests==2.11.1
Let's create another virtualenvs and check if requests was installed.
mkvirtualenv test_virtualenv_2
pip freeze | grep requests
After execute pip freeze | grep requests no output is expected. That's all, hope you learnt a new very usefull tool!