Python virtualenv – Virtual Environments

 

What is virtualenv

A Virtual Environment is a tool for python to keep the dependencies required by different projects isolated in separate places, by creating virtual Python environments.

It solves the problem “ProjectX depends on libraryA of version 1.0 but ProjectY needs library of version3.0” dilemma. Keeps the global site-packages directory clean and manageable.

Without virtualenv you would need to install/uninstall dependencies while you switch between projects that are having dependencies of same libraries but different versions.

For example, with virtualenv you can work on a project which requires Django 1.8 while also maintaining a project which requires Django 1.6.

Install virtualenv

The virtualenv is a tool to create isolated Python environments. virtualenv creates a folder which contains all the necessary executables and packages that a Python project would need.

Install virtualenv using pip:

 

virtualenv syntax

How to use virtualenv

  1. Create a virtual environment for a project:

Command virtuale venv will create a folder in the current directory with name venv and will put all necessary python libraries required to work virtual environment isolated. That is, which will contain the Python executable files, and also  the pip library where you can use to install other packages.

Here the name of the virtual environment is venv  as we have given that name to virtualenv command. If you don’t specify a name or directory, current directory will be used to create virtual environment.

You can also specify a Python interpreter of your choice.

This command will tell python virtualenv to use  the Python interpreter from location specified /usr/bin/python2.7 

  1. To Start using the virtual environment, it needs to be activated:

To activate virtual environment, we use the command source with argument as a file activate which will reside in bin directory of created virtual environment directory. The name of the current virtual environment will now appear on the left of the prompt (something like  (venv)User@HostName:/path/to/directory$ ) to let you know that it is activated. As long as this virtual environment activated, any package that you install using pip will be placed in the venv folder, isolated from the global Python installation.

You can Install packages as usual using pip:

  1. Once you are done working with virtual environment, you can deactivate it:

deactivate will be aviable as a bash command once you activate virtual environment. It can be used to deactivate(exit from) virtual environment.

So, this puts you back to the system’s default Python interpreter with all its installed libraries.

If you want to  delete a virtual environment, just delete its folder. (here in this case, it would be rm -rf venv.)

virtualenv Options

Creating virtual environment with the option --no-site-packages will not include the packages that are installed globally. This can be useful for keeping the package list clean in case it needs to be accessed later. [This is the default behavior for virtualenv 1.7 and later.]

Freeze Requirements

In order to keep your environment consistent or reinstall virutal environment or distriubute as package, it’s a good idea to have a list of dependencies/requirements. You can “freeze” the current state of the environment packages. To do so, run

This will create a requirements.txt file, which contains a simple list of all the packages in the current environment, and their respective versions. You can see the list of installed packages without the requirements format using “pip list”. Later it will be easier for you or other  developer to recreate or reuse environment and install dependencies.

This can help make sure consistency across installations, across deployments, and across developers.

Other environment virtualization tools for python

  • p – Dead simple interactive Python version management.
  • pyenv – Simple Python version management.
  • venv – (Python standard library in Python 3.3+) Creating lightweight virtual environments.
  • virtualenvwrapper – A set of extensions to virtualenv.

Leave a Reply

Your email address will not be published. Required fields are marked *