When we install site packages in python (using pip or easy_install), they are placed in directories pointed to by site.getsitepackages()
>>> import site >>> site.getsitepackages() ['/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.5/dist-packages']
Now, suppose you are working on two projects project A and project B, but A requires version 2.x of a library C while project B requires version 1.X of the same library. And if you install both the versions, both pf them will, by default, get stored in the dist-packages/site-packages directory under the same name C. So you cannot use two versions of the same library. This is where python virtual environments come in handy. For example you can work on a project which requires Django 1.10 while also maintaining a project which requires Django 1.8.
What is python virtual environment?
At its core, the main purpose of Python virtual environments is to create an isolated environment for Python projects. This means that each project can have its own dependencies, regardless of what dependencies every other project has.
virtualenv is a tool to create isolated Python environments.
Install virtualenv using pip
$ sudo pip install virtualenv
- Create a virtual environment for a project
$ virtualenv proj_name
This will create a folder in the current directory which will contain the Python executable files, and a copy of the
pip library which you can use to install other packages. The name of the virtual environment will be
You can also use the python interpreter of your choice
$ virtualenv -p /usr/bin/python2.7 proj_name
or you can change the interpreter globally with an env variable in
$ export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2
- Activate the virtual environment
To begin with using the virtual environment it needs to be activated.
$ source proj_name/bin/activate
The name of the current virtual environment will now appear on the left of the prompt (e.g.
(proj_name)Your-Computer:your_project UserName$) to let you know that it’s active. From now on, any package that you install using pip will be placed in the
proj_name folder, isolated from the global Python installation.
- Deactivate virtual environment
If your work on the virtual environment is done you can close if using
This puts you back to the system’s default Python interpreter with all its installed libraries.
To delete a virtual environment, just delete its folder. (In this case, it would be
rm -rf proj_name)
While virtual environments certainly solve some big problems with package management, they’re not perfect. After creating a few environments, you’ll start to see that they create some problems of their own, most of which revolve around managing the environments themselves. To help with this, the
virtualenvwrapper tool was created, which is just some wrapper scripts around the main
$ pip install virtualenvwrapper $ export WORKON_HOME=~/Envs $ source /usr/local/bin/virtualenvwrapper.sh
- Create virtual environment
$ mkvirtualenv proj_name
This creates the proj_name folder inside ~/Envs.
- Work on a virtual environment
$ workon proj_name
$ rmvirtualenv venv
You can view the full list of commands here.