virtualenv can create isolated Python environments.

Python by default install modules system wide. That can become an issue if programs need different versions of the same module.

This is unlike other programming languages that don't install modules system wide.
Imagine two Python apps of which one needs libBar 1.0 and another libBar 2.0.

A virtualenv solves this problem cleverly by creating an isolated environment. Modules will only be installed inside the virtual environment. Inside your environment you can install any module without affecting the systemwide configuration.

Related course: Complete Python Programming Course & Exercises

Setup

The program virtualenv comes to the rescue. It lets you create virtual environments.
To create a virtual environment use the command:

1
virtualenv -p python3 envname

where envname is your project name. If we name the project ג€œtestprojectג€ we get this line:

1
virtualenv -p python3 testproject

This will create the folder virtualenv with these sub directories: bin, include, lib and share.

To load your virtual environment type

1
2
cd testproject
bin/activate

Your virtual environment is now activated.
The shell will show the current virtual environment is loaded.

virtualenv python

To return to the normal environment, type

1
deactivate

You can create as many virtual environment as you want. Every project should have its own virtualenv because it needs specific modules that need not be installed system wide.

virtualenv showing modules

If you are a beginner, then I highly recommend this YouTube Python Playlist.

Exercise

Try the exercises below:

  1. Setup a new virtual environment for a project and activate it. Install a module using pip.