Python Virtual Environment

By defualt Pyton only gives you a single environment for your whole system.  This means when you install Python, or a package using Pip, you are installing that version of python or that version of the python package for the entire system.  And all scripts you run will interact with that same version of python or package. However, there are benefits to having a different python environment for each python application. Each application may have different dependencies. And may have compatibility issues with future versions of python packages.  If this sounds intriguing to you, you should check out virtual environments.

Installing VirtualENV

VirtualENV is a python package that gives you the ability to create a new python environment in each folder. It is easy to install if you already have pip installed. For information about installing pip, see our article on python basics.

Once you have pip installed, you can install virtualenv by running:

Or, you might have to run:

On Windows, you may need to run this from an elevated command prompt

Using VirtualENV

When using Python without virtualenv, everything is installed system wide. That is why you sometimes have to elevate your permissions to install or upgrade packages via pip. Virtualenv turns each folder into an isolated python environment. This alleviates the permissions issues for one. But it also gives you the ability to use different versions of packages in each environment.

To get started open a new command prompt or termianl, then create a new folder called project1 (mkdir project1)
type cd project1 to enter that folder

This will create the new virtual environment for your project

create virtualenv environment

If you type ls or dir (depending on OS), you will see that there is now a folder called env. This folder contains all of the binaries for your new environment. If you look in the env/bin folder, you will see that there are copies of easy_install, pip, python, etc… within this folder.

**Note: If you are including this folder in source control, it would be good to add the env folder to your .gitignore file.

Using Environments

Navigate into your project1 folder, and create a file called within, add the text:
import requests

save your fle, then go bak to your terminal/command prompt, navigate to your project1 folder, and run:

This command is using the copy of python in your newly created environment to run your test ptyhon script.

You should get an error similar to the one below:

python import error no module named requests

You are seeing this error because in your newly created environment, you don’t have any modules installed. You can install the requests module by typing

After installing requests, run your test1 script again:

You shouldn’t get any more errors.

To verify everything is working as planned, lets switch to our project2 folder, and create a new script called, then add import requests t othe top.
Next run:

you should once again get the import error about the requests module:

project 2 import python import error

The error above demonstrates that what you do in project1 is independent of what you do in project2. Now you can create a new environment every time you have a new project and you won’t have to worry about one projects dependencies stepping on your second projects dependencies.

%d bloggers like this: