How to set up Virtual Environments in Python
By: Adhyantini Bogawat
Tags: Python, Python3, Virtual Environment, Python Programming, Programming

How to set up Virtual Environments in Python

What are Virtual Environments?

We all are aware of the term Environment and its meaning: It is nothing but a surrounding in which we operate. Python too has its own concept of virtual environments. To begin with, let’s consider the example of a refrigerator, it is used to store food at cool temperatures. However certain food items such as meat require to be stored at cooler temperatures. Therefore the refrigerators has its own isolated “Environment” also known as a deep freezer where the meat can be stored. Hence, although the refrigerator does the task of cooling, there are some items which have different requirements and need a separate storage of its own within the refrigerator. Even if the temperature of the refrigerator changes, the temperature of the deep freezer will remain the same.

Photo by Dev Benjamin on Unsplash

Virtual Environments in Python work in a similar fashion. Although your main environment has installed all latest packages that are required, certain projects may not work with those versions of packages and may fail to execute. It has its own requirements. This is where the use of virtual Environments comes into the picture.

Technically speaking, the main purpose of Virtual Environments in python are used for creating isolated environments needed by python projects. Here, unlike refrigerators where there’s only one environment, in python, we can create as many virtual environments as needed! They are nothing but directories that contain a few scripts. And these are easy to create too!

Why should we use Virtual Environments?

Python has its own standard package manager called “pip” . We have always made use of this command while installing third party packages. What pip essentially does is looks for the latest version of these packages and then stores it in a directory. All python projects on our system will then refer to this directory to make use of the third party libraries.

Alright, but why use virtual environments?

Now Suppose, you have two projects, say Project “Basketball” and Project “Football”. Both projects rely on the use of a certain version of a third party library, say “Ball”. If project “Basketball” has used version 1.00 for development, and the library has hence been upgraded to a version 2.00 , and v2.00 is made use by project “ Football”, this would mean that project “Basketball” too has to use the same v2.00 since v1.00 is stored in the same directory as the v2.00 and python cannot differentiate between the two versions. This could give rise to code breakage due to different dependencies!

To resolve this issue is why the need of virtual environments is so important. What virtual environments do is create separate isolated directories of different projects and include all the packages that are separate from the system packages, and specific to that project only. This would mean that Project “Basketball” and Project “Football” would then have their dependencies stored separately and would not cause any problems.

Now that we have seen all about the “what” and “why” of virtual environments, let’s dive into how we can create these. You will be glad to know that it’s quite simple and can be created easily using the command line using the venv tool!

Note: Your main project should not be stored inside the virtual Environment files. It should only store the packages that will be required for the project.

Getting started with Virtual Environments

Linux

Before we start working with virtual environments, make sure pip is installed on your system. If it is not, use the following command:

$ sudo apt-get install python3-pip

Now lets install virtualenv using pip.