Python virtualenv Guide

Posted on Oct 31, 2019


note:

This tutorial need pip, if you have not already done so, first go through installing pip.

virtualenv is a tool used to separate different dependencies required by the projects. While working on multiple projects it's a common issue that one project need a version of package that is completely different from the other one, virtualenv helps us to resolve such kind of issues. It also helps to prevent polluting global site package.

Installing virtualenv #


virtualenv is just a package available at pypi, you can use pip to install virtualenv.

pip install virtualenv

After installation you may need to add C:\Python34\Scripts to your PATH environment variable. This way commands like pip, virtualenv will become available in any directory level.

Creating a Virtual Environment #


Create a new directory called python_project and change current working directory to python_project.

1
2
mkdir python_project
cd python_project

To create a virtual environment inside python_project you need to issue the following command.

virtualenv my_env

This will create a new folder my_env inside python_project. This folder will contain a copy of python executables and pip library used to install packages. Here we have used my_env as name, but you can use anything you want. Now your virtual environment is ready to use, you just need to activate it.

There is one point in this tutorial we have installed virtualenv using python 3.4 suppose you also have python 2.7 and want to create a virtual environment that use python 2.7 instead of 3.4, you can do so using the following command.

virtualenv -p c:\Python27/python.exe my_env

Activating Virtual Environment #


If you are on windows you need to execute the following command.

my_env\Scripts\activate.bat

On Linux enter this.

source my_env/bin/activate

After issuing the above command your command prompt string will change and will look something like,

( my_env ) Path_to_the_project: $

Notice ( my_env ), this indicates that you are now operating under virtual environment.

Now you virtual environment is activated. Anything you install here will be used by this project only.

Let's try to install requests package.

In Windows enter the following code.

my_env\Scripts\pip.exe install requests

You can't use use just pip install requests in windows because it would execute the global pip if you have added C:\Python34\Scripts to your PATH environment variable. If you have not added then you will get an error.

Similarly, in Linux you need to execute the following code

my_env\Scripts\pip install requests

Deactivating Virtual Environment #


To deactivate virtual environment you need to use the following command.

deactivate

This command will put you back in system's default python interpreter, where we can install the package in the global site package.

You should now able to see the motivation behind using virtualenv. It helps us to organise the needs of projects without conflicting with each other.


Other Tutorials (Sponsors)