Python virtualenv Guide

Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

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.

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 .

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

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.

Activating Virtual Environment

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

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,

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.

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

Deactivating Virtual Environment

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

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

This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

Leave a Reply

7 Comments on "Python virtualenv Guide"

Notify of

Sort by:   newest | oldest | most voted
9 months 12 days ago

So I have done the install for virtualenv but when I try to create my folder this is what I get any ideas? tried to look on stackflow.

desktop duwaynew$ virtualenv someone -bash: /usr/local/bin/virtualenv: /usr/local/opt/python/bin/python2.7: bad interpreter: No such file or directory

What does this mean?

10 months 1 day ago

Thanks my friend, this blog post is useful 🙂

Anurag mishra
11 months 9 days ago

How to add Path environment variable C:\Python34\Scripts I did it by
MyComputer > Properties>Advanced System Properties>Environment variable

variable name : add
variable value : C:\Python34\Scripts

[I am a windows 8.1 user]

please help me while writing this command my_env\Scripts\pip.exe install requests i get app is not defined. How to add path environment variable??