Virtual Environment in Python

Have you ever heard of virtualenv? If you are a beginner, then you might not have heard about it but if you are a seasoned programmer then it may well be a vital part of your toolset.

So what is virtualenv? Virtualenv is a tool which allows us to make isolated python environments. Imagine you have an application that needs version 2 of a library, but another application requires version 3. How can you use and develop both these applications?

If you install everything into /usr/lib/python2.7/site-packages (or whatever your platform’s standard location is), it’s easy to end up in a situation where you unintentionally upgrade a package.

In another case, imagine that you have an application which is fully developed and you do not want to make any change to the libraries it is using but at the same time you start developing another application which requires the updated versions of those libraries.

What will you do? Use virtualenv! It creates isolated environments for your python application and allows you to install Python libraries in that isolated environment instead of installing them globally.

To install it, just type this command in the shell:

.. code:: python

$ pip install virtualenv

The most important commands are:

  • $ virtualenv myproject
  • $ source bin/activate

This first one makes an isolated virtualenv environment in the myproject folder and the second command activates that isolated environment.

While creating the virtualenv you have to make a decision. Do you want this virtualenv to use packages from your system site-packages or install them in the virtualenv’s site-packages? By default, virtualenv will not give access to the global site-packages.

If you want your virtualenv to have access to your systems site-packages, use the --system-site-packages switch when creating your virtualenv like this:

.. code:: python

$ virtualenv --system-site-packages mycoolproject

You can turn off the env by typing:

.. code:: python

$ deactivate

Running python after deactivating will use your system installation of Python again.

Virtualenvwrapper

Virtualenv wrapper is a set of extensions for virtualenv. These extensions make your life easier and improve your deveopment workflow. The features of virtualenvwrapper are (taken from project page):

  • Features
  • Organizes all of your virtual environments in one place.
  • Wrappers for managing your virtual environments (create, delete, copy).
  • Use a single command to switch between environments.
  • Tab completion for commands that take a virtual environment as argument.
  • User-configurable hooks for all operations (see Per-User Customization).
  • Plugin system for more creating sharable extensions (see Extending Virtualenvwrapper).

Most importantly virtualenvwrapper stores all your virtual environments in one place and allows you to shift from one environment to another quite easily. You can install it by running the following commands:

$ pip install virtualenvwrapper
$ export WORKON_HOME=~/Envs
$ mkdir -p $WORKON_HOME
$ source /usr/local/bin/virtualenvwrapper.sh

Now you can create your first environment by running:

$ mkvirtualenv venv1

You can see that it installed the virtual environment in the ~/Envs directory by running the following command:

(venv1)$ ls $WORKON_HOME
venv1 hook.log

Now you can install the software in the usual way by using pip:

(venv1)$ pip install flask

You can create multiple environments and shift from one to another quite easily:

(venv1)$ mkvirtualenv venv2
(venv2)$ workon venv1

You can even copy virtual environments:

(venv1)$ cpvirtualenv venv1 dest

The above command creates a new virtual environment with the name of dest. Lastly you can deactivate a virtual environment by running:

deactivate

There are a lot of other cool virtualenwrapper commands which I did not list here. You can check them out over here. I hope you liked this short intro to virtualenv and virtualenvwrapper.