What is python virtual environment and how to create it in linux?

This time we are going learn about python virtual environment and how to create one using python3. If you code in python then you must be aware of pip package installer which allows you install python packages available on pypi.org using your command line interface. If you have used any python script downloaded from github repository then you must have seen a file “requirements.txt”. This file contains a list of all the python dependencies which are required to run the script. Suppose a python script you have downloaded from github needs requests==2.20.0 to be installed in your system. So you can easily install the required version in your system using command

pip3 install -r requirements.txt

Above command will install all the required dependencies inside your python’s library location which is “/usr/local/lib/python<version>/dist-packages” in case of linux.

Now you have another python script which requires requests==2.0.0, when you install the requests==2.0.0 version then there might be chances that this could break your first script which needs requests==2.20.0

Here the virtual environment comes into play, virtual environment allows you to isolate the dependency installation of a python application/script. That means you don’t need to install the dependencies in your system rather you install all the required dependencies inside a virtual environment where your application resides and when you run the python application/script all the dependencies will be picked from the virtual environment rather than from your system’s python libraries path. Let’s see how to make a python virtual environment 😉

This is a simple script which is using requests module. Now when I run this script my system will show an error because I don’t have this module installed in my system right now.

Now let’s create a virtual environment(using module venv) where the request module and it’s dependencies will be installed.

Now after activating the virtual environment (notice name of the virtual environment inside brackets) when you use pip3 command to install a module then all the modules will be installed inside your virtual environment.

To check the modules installed in your virtual environment use command

pip3 freeze

As you can see only pkg-resources==0.0.0 is listed. Now let’s install requests module and then list all the installed modules.

You can see in above image that requests module is installed using command

pip3 install requests

Above command will install the latest version by default but for specific version you can mention the required version. for ex — requests==2.20.0

Now when you list all the installed packages using command pip3 freeze you can see the requests module has been installed successfully along with all the dependencies. All these packages are installed in virtual environment we have created for our app.py script.

Now Our script has started working 😎😎😎😎😎

Now suppose you have two python applications which are using different versions of same module. You can create virtual environments for each application and install the required version for each of them without breaking any of the application script.

Thanks for reading this post, hope you learned something from it.

An avid learner in the field of information security. A self learner and a ctf player sometimes.