Python @ DjangoSpin

Creating isolated Python environments using virtualenv

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 4 minutes

virtualenv in Python

virtualenv in Python

Creating isolated Python environnments is a great industrial practice to keep two or more applications separate from each other. These applications have different dependencies, e.g. one may use 1 version of an imported library and the other may use another version of it. To keep both these applications stable and decoupled from each other, it's best to create two different Python environments to avoid the hassle of why-is-this-application-running-and-why-is-this-not.

The Cheese Shop provides a library called virtualenv which helps the developers to create separate Python environments. Using this library, you can create a standalone Python environment for a project with a simple command. This standalone copy of Python has pip with it, which you can use to install libraries you need only for this project. These libraries will not be installed in the global copy of Python you have on your system. In order to recreate the same environment on another system, virtualenv enables us to create a text-file of all the installed libraries which you can provide to the new environment during its creation, and it will install them one-by-one on its own. Furthermore, virtualenv also allows you to choose which version of Python you want to work with by specifying it during environment creation command.

Let's go over all these steps in details. The $ in the below commands indicates that these are executed in the command prompt.

Installing virtualenv using pip

virtualenv can be installed using Python's out-of-the-box package manager pip.

$ python -m pip install virtualenv

The -m flag tells Python to run the specified module as a script.

Creating a virtual environment

Once virtualenv is installed, navigate to the folder where your application is situated. Open it, and press Shift + Right Click, click on Open command window here. In the popped-up command window, enter:

$ virtualenv virtualEnv01

This command will create a directory virtualEnv01 containing the Python executables along with a copy of pip, to facilitate installation of required libraries. The name specified virtalEnv01 is the name of our virtual environment, and you can name it anything you want.

Activating the Virtual Environment

Once our virtual environment is created, we need to tell the operating system that we will be working with this copy of Python environment. To do so, in the same command prompt, with curent directory as the directory where your virtual environment is placed:

$ cd Scripts
$ activate.bat


$ cd Scripts
$ activate

You will notice that the name of your virtual environment i.e. virtualEnv01 will appear before the prompt e.g. (virtualEnv01) path_to_virtualEnv01\virtualEnv01\Scripts>. This indicates that it is activated now. Any library you install using pip will be installed in this environment. You can install a library using standard pip syntax i.e.

$ (virtualEnv01) path_to_virtualEnv01\virtualEnv01\Scripts>cd ..
$ (virtualEnv01) path_to_virtualEnv01\virtualEnv01\> pip install cx_Oracle

Deactivating The Virtual Environment

When you have finished working on the current application, you can deactivate the virtual environment in order to return to the global installation of Python, or to activate another virtual environment.

$ (virtualEnv01) path_to_virtualEnv01\virtualEnv01\>cd Scripts
$ (virtualEnv01) path_to_virtualEnv01\virtualEnv01\Scripts> deactivate.bat

$ (virtualEnv01) path_to_virtualEnv01\virtualEnv01\>cd Scripts
$ (virtualEnv01) path_to_virtualEnv01\virtualEnv01\Scripts> deactivate

Deleting a Virtual Environment

If you wish to remove a virtual environment from disk, you can do so by removing the folder from disk.

Exporting and Importing Requirements

To obtain a list of all libraries you have installed using pip in the current virtual environment, you can use the freeze & list options of the pip command. The list option gives a list of the libraries, and the freeze option gives the versions along with each library name. With the virtual environment activated, execute the following commands:

$ pip list

$ pip freeze

In order to redirect the output to a text file, you can use the > operator.

$ pip freeze > requirements.txt

In order to setup a new virtual environment from this list, you can use the -r flag of the pip command to install each of these libraries.

$ pip install -r requirements.txt

Setting up the virtual environment with a different version of Python

Suppose you are using Python 2 for developing a 2-D game using Pygame, and you are using Python 3 for making a blog with Django 1.8. If you have multiple versions of Python installed, you can specify which version of Python will be used in the virtual environment. This can be done by using the -p flag of virtualenv while creating the virtual environment.

$ virtualenv -p <path_to_particular_python.exe> virtualEnv01

For example, if you have 2.7 as well as 3.4 installed on your system, and Python executable of version 2.7 is located at C:/Python27, then put this path in the above command, i.e. :

$ virtualenv -p C:\Python27\python.exe virtualEnv01

Managing All Virtual Environments

Once you start to work with virtual environments, you will notice that you will have many different virtual environments lying around your system. It might even get difficult for you to find a particular application. The Cheese Shop offers another utility, virtualenvwrapper, which enables the user to manage different environments. It offers commands which help in dealing with multiple environments. It even provides tab-completion for environment names, which is really handy when you are dealing with many environmnets.

Installing virtualenvwrapper

It is available for download from The Cheese Shop. You can install it in the global Python installation using pip.

$ python -m pip install virtualenvwrapper

There is an alternative package in The Cheese Shop for Windows, by the name of virtualenvwrapper-win.

$ pip install virtualenvwrapper-win

Creating a Virtual Environment Via virtualenvwrapper

The mkvirtualenv command creates a virtual environment virtualEnv01 in the Envs directory.

In Windows, the default path for Envs is %USERPROFILE%Envs.

$ mkvirtualenv virtualEnv01

Upon execution of the above command, you will find the virtualEnv01 directory inside Envs. Also, you will notice that the provided virtual environment will be activated automatically.

Activating & Deactivating a Particular Virtual Environment

Use the workon command in command prompt to activate a particular environment.

$ workon virtualEnv01

You can shift environments by specifying a different environment name in the workon command. This will automatically deactivate the earlier environment and activate the new one. Alternatively, you can use the deactivate command to deactivate the current environment.

$ deactivate

Deleting a Particular Virtual Environment

You can use the rmvirtualenv command to remove a particular virtual environment. The associated directory will be removed from the Envs directory.

$ rmvirtualenv virtualEnv01

The Documentation of virtualenvwrapper lists a host of useful commands that you can use. It's worth a read if you are going to use the tool.

See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply