Oct 29 2021 · Piotr Płoński

Python Virtual Environment Explained

Python Virtual Environments ExplainedHave you ever messed with Python packages? Have you ever had problems running someone else script because of broken dependencies? Would you like to know why and how to fix it? In this article, we explain how does Python virtual environment work.

We will start by introducing the main characters in our story.

Starring

House - the operating system

In our story, the House will be an operating system. It has three floors. Each floor represents some path in the folder directory. The lowest floor is a default path. When someone enters the House, always checks this path.

House as operating system

Power Station - Python interpreter

The Power Station will give energy to our Robots. The Power Station represents here the Python interpreter. Would you please notice that our Power Station has version 3.8? It corresponds to the Python version (3.8). What is more, our Power Station has a lamp. If the lamp has a light, then it means that the Power Station is on.

House as operating system

Robots - Python packages and scripts

There will be a lot of Robots in our tale. The Robot is a Python program. It can be a package or a script.

Robots as Python scripts and packages

Robots will do hard work. They don't complain. There are two types of Robots in our story: scripts and packages. Below is the example of a Robot with a script. It has the name script.py. It has some code blocks in the body. Please notice that our Robot has two hands. The number of hands is unlimited. It can be 0 or 10 (any number) - it depends on the Robot. Each hand can hold the other hand, but only if it fits. Our script Robot needs other Robots to work. They will hold his hands.

Robot as Python script

The particular case of the hand is the hand that fits a human hand. In that case, we can say that Robot has a friendly interface for humans (for example, it can be a command-line interface or graphical interface). Do you see how happy the human with a Robot friend is?

Robot with Human

The second type of Robots is a package. Below we have a Robot representing package B. It has a version 2.1.3 and a hand. Would you please notice that hands can be in different shapes?

Robots as Python scripts and packages

Please remember that only Robots with the fitting hands can be connected. The next thing, the Robot script needs to have all hands connected to be able to work.

Power Station installation on the first floor

Let's start. We have an empty House. We bring a Power Station to our House (we install Python). We will install it on the first floor because it is our default floor. The lamp is on, which means that Power Station is active.

Python installation

What's happening? Nothing. We need Robots to see action.

Add Robot script

OK, we start by creating a script Robot with the name script.py. It will enter the House and go to the first floor.

First Python script

Our first script is straightforward (it doesn't have any dependencies). For example:

print("Hello world!")

Does it do a lot of work for us? No. It will just print Hello world! on the screen.

To have more done with the Robot, we need to add more code to our script. We will not write everything from scratch. We will reuse existing packages. We will use packages A (version 1.0.0) and B (versio 2.1.3). We need to install the required packages - we need to bring them to our default floor.

Extended Python script

OK, now our script is doing a lot of work and using two external packages. We are happy.

A turning point - next Robot script

But one day, we need to create one more Robot script (with name onemore.py). This time, we will need to use a newer version of package A - version 1.5.7 (with new features). Unfortunately, starting from version 1.5.0, all Robots A has a different hands shape. Before the 1.5.0 version, all Robots A have diamond hands. But, starting from 1.5.0, all Robots A have circle shape hands.

Extended Python script

What does it mean for us? We can't just replace the Robot A 1.0.0 on the first floor because it will break the Robot with script.py. The Robot with script.py has diamond shape hand, it won't fit circle hand from version 1.5.7.

Do we need to choose between Robots script.py and onemore.py? Can we use only one? No, we will use virtual environments to keep both versions.

Install Power Station on the second floor

We have many empty floors in our House. Let's install one more Power Station on the second floor. It will be our Python virtual environment.

Python installation in the second floor

Do you notice that the lamp is off? After installation, we need to activate the Power Station on the second floor. It will deactivate the Power Station on the first floor.

Activate Python in the second floor

Now we can bring (install) Robot with the required packages. We will install package A in version 1.5.7 and create a Robot script onemore.py.

one more Python script in the second floor

The Power Station on the second floor represents a Python virtual environment. It can be switched on when needed. It has different packages with different versions than the first floor (default Python installation).

Install Power Station on the third floor

Let's create one more Robot script with different packages versions. We will have Robot script last.py with required packages: A in version 2.1.0 and B in version 4.1.0. In the image below, we show how packages change with versions.

Packages version change

No problem, just bring one more Power Station (create one more Python virtual environment). We will install the Power Station on the third floor. We can safely place packages A and B in a different version without worrying if we break something on the other floors. Remember to switch on the lamp after Power Station installation (activate the environment).

Last Python script in the third floor

Leaving the House and more Robot scripts

What will happen if we leave the House and come back after some time (switch off and on the computer)? All Power Stations on higher floors will be off (virtual environments will not be active). Only Power Station on the first floor (in the default path) will be on. If we want to use the Robot script, it will try to use packages from the first floor. So, if we need other sets of packages or versions, we need to activate the Power Station on the proper floor (switch to the virtual environment).

Summary

When using Python packages, a programmer needs to be very careful about the versions. Python virtual environments are an excellent solution to avoid conflicts. We hope that analogy to House, Power Station with a lamp and Robots will help you visualize and understand how to leverage Python virtual environments.

We are looking for feedback or comments from you. You can reach us by email to Ola.

We are working on the MLJAR Studio desktop application. It is a new notebook for creating Python scripts with no-code addons. The application is still under development. If you would like to be informed about the release please fill the form.