A virtual environment is a self-contained directory tree that contains dependencies required by different projects isolated to existing packages. By using a virtual environment for python, applications can run in their own ‘sandbox’ in isolation of other Python applications.

 

Why use Virtual Environment for python?

The virtual environment creates a directory that contains dependencies required by different projects along with some scripts. There are no limitations to the number of environments you can create.

Now let’s take a real-world example, imagine you have two projects A and B which depend on module C. The problem appears when we need different versions of module C, let’s say module A demands v 1.0, and project B demands V 3.0 for some compatibility reason. But python won’t be able to differentiate between the different versions of a module because both the version will be stored in the site package directory with the same name.

This is where you should use the virtual environment to create separate virtual environments for both the projects, different environments can have different versions of modules. It is a good practice to have a new virtual environment for every Python project. This tutorial will guide you through how to create a Virtual Environment for your project.

 

How to create a Virtual Environment for python?

Step 1: Open your terminal and create a directory to store all your virtual environments, using the command  mkdir Environments which is an acronym of “make directory”. Now go inside the directory using the command CD which stands for call Directory,  CD Environments

Step 2: Now we will use a module named virtualenv to create isolated virtual environments. But first, let’s install this module by the following command,

If you get an error like pip command not found then you have to install pip package manager first, you can learn this here. To verify a successful installation run this

Now we can proceed to create virtual environments. We will create a virtual environment named myenv.

 

How to create Virtual Environment if you have two different versions of Python?

To create a Virtual Environment for Python 2.x do the following

For a Python 3 virtual environment type –

 

If you only have Python 3 on your machine do the following

This will also work,
This will create a directory myenv along with directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files. If you don’t know the difference between python 2 vs python 3, read here.

 

Create global virtual environment for Python

A virtual Python environment has a similar directory structure to a global Python installation. The bin directory contains executables for the virtual environment, the include directory is linked to the global Python installation header files, the lib directory is a copy of the global Python installation libraries and where packages for the virtual environment are installed, and the shared directory is used to place shared Python packages.

The interesting thing here is that these environments won’t have any existing Python package or module of your computer, you can verify it by navigating into myenv>Lib>site-packages. You will only find the essential tools such as pip, easy install, and setup tool, later this directory will hold all the packages and modules you will install in this particular environment and remain isolated from other environments.

 

How to activate the virtual environment for Python

To add modules and packages to our Environment, we need to activate it first. Now your command prompt will be prefixed by the Environment name which is, in this case, myenv. This indicates that our Virtual Environment has been activated.

On Windows, use the following run: myenv\Scripts\activate.bat
On Unix or macOS, use the following run: source myenv/bin/activate

 

How to deactivate the virtual environment for Python

You can install any package here with pip or easy install these packages will be completely isolated to other environments on your device. To deactivate the environment run the following

Now there is no more a prefix in our terminal, which indicates that our environment has been deactivated successfully.

Leave a Comment