How to Create and Use a Virtual Environment in Python in Ubuntu 22.04

kinyungu_denis

Kinyungu Denis

Posted on November 1, 2022

How to Create and Use a Virtual Environment in Python in Ubuntu 22.04

Introduction

Greetings my esteemed readers. I took a break to participate in HacktoberFest with Aviyel, it was an awesome experience, I really learnt a lot and I will be sharing with you my dear readers. I am happy to be back and share my knowledge with you.

We will learn about Virtual environments in Python,how does one create a virtual environment, why should one create a virtual environment and how to manage a virtual environment. In order to get the best out of this article, you should understand the basics in Python.

What is a Python virtual environment?

Python Virtual Environment is an environment that ensures packages related to different projects are stored in different places to avoid dependency conflicts, it is isolated from other environments and allows that environment
to have its own independent dependencies.

A single Python installation can fail to meet the requirements of every application. If application K needs version 4.0 of a particular module but application W needs version 3.0, then the requirements are in conflict and installing either version 3.0 or 4.0 will leave one application unable to run.

The solution for this problem is to create a virtual environment so that different applications can then use different virtual environments and are able to run in your system.

How to create a virtual environment?

We will use venv to manage separate packages for different packages.

To create a virtual environment go to your projects directory and run venv. For example in my case I will navigate to my required directory using cd command:

cd /home/exporter/Kadenno/python_projects/Django_projects/project_one
Enter fullscreen mode Exit fullscreen mode

Navigating to project directory

After running the above command we are in our desired project directory.
Now we can run our venv command as follows:

python3 -m venv env
Enter fullscreen mode Exit fullscreen mode

Using venv in virtual environment

We use the second argument env as the location for our virtual environment however you can change it in case you want to have a location of your own.

Basically venv will create a virtual installation in the env folder.

Now we need to activate our virtual environment. Before you begin installing and using your packages, you virtual environment should be activated. Now we will activate our virtual environment as follows.

source env/bin/activate
Enter fullscreen mode Exit fullscreen mode

Activating our vrtual environment

Great, now we have our virtual environment up and running, we can go ahead and install our required packages for our project.

You are able to confirm whether you are in your virtual environment by using the following command:

which python
Enter fullscreen mode Exit fullscreen mode

Checking whether in virtual environment

When you have completed your project, you can leave your virtual environment by using the following command:

deactivate
Enter fullscreen mode Exit fullscreen mode

For our case, we wont leave our virtual environment since we are yet to complete our project.

To illustrate an example of a package we can install and use, let us upgrade our pip in our virtual environment.
You will use the following command:

pip install --upgrade pip
Enter fullscreen mode Exit fullscreen mode

Installing pip in our virtual environment

This looks awesome, you can proceed to install other packages, libraries and dependencies in the virtual environment which you will require.

At this this point, we understand what is a virtual environment, how one can create it and how you can deactivate it. However, do you know how a virtual environment works?
Let's take a deep dive and learn about it.

How does a virtual environment work?

The virtual environment representation

When you create a virtual environment using venv, the module re-creates the file and folder structure of a standard Python installation on your operating system. Python also copies or symlinks into that folder structure the Python executable with which you’ve called env.

It Adapts the Prefix-Finding Process:
Standard folder structure, the Python interpreter in our virtual environment can understand where all relevant files are located. It does this with only minor adaptations to its prefix-finding process according to the venv specification.

Instead of looking for the os module to determine the location of the standard library, the Python interpreter first looks for a pyvenv.cfg file. If the interpreter finds this file and it contains a home key, then the interpreter will use that key to set the value for the following two variables:

  • sys.base_prefix: hold the path to the Python executable used to create this virtual environment, which you can find at the path defined under the home key in pyvenv.cfg.
  • sys.prefix: point to the directory containing pyvenv.cfg.

If the interpreter doesn’t find a pyvenv.cfg file, then it determines that it’s not running within a virtual environment, and both sys.base_prefix and sys.prefix will then point to the same path.

It links back to Your Standard Library:
Python virtual environments aim to be a lightweight way to provide you with an isolated Python environment. In that you can quickly create and then delete when you don’t need it. venv copies only the minimally necessary files.

The Python executable in our virtual environment has access to the standard library modules of the Python installation on which you based the environment. Python points to the file path of the base Python executable in the home setting in pyvenv.cfg

It modifies Your PYTHONPATH:
Scripts should run using the Python interpreter within our virtual environment, venv modifies the PYTHONPATH environment variable that you can access using sys.path.

It changes Your Shell PATH Variable on activation:
You activate your virtual environment before working in it. To activate your virtual environment, you need to execute an activation script, just as how we activated.

Actions that happen in the activation script:

  • Path: It sets the VIRTUAL_ENV variable to the root folder path of your virtual environment and puts the relative location of its Python executable to our PATH. The path to all the executables in your virtual environment now lives at the front of your PATH, when you type python or pip our shell invokes their internal versions.
  • Command prompt: command prompt will the name that you passed when creating the virtual environment. It takes that name and puts it into parentheses, for example (env). We saw when we were created our virtual environment.Our command prompt, you will know whether or not your virtual environment is activated.

You will activate your virtual environment before working with it and deactivate it after you’re done, as we discussed earlier.

Conclusion

Through this article we have learnt about virtual environment in Python. Virtual environments give you the ability to isolate your Python development projects from your system installed Python and other Python environments. This gives you full control of your project.

When developing applications that would generally grow out of a simple .py script, it's a good idea to use a virtual environment. Reading through this article you now know how to set up and start using one.

Let me know what you think about this article through my Twitter or LinkedIn handles. It would be great to get your feedback and connect with you.

💖 💪 🙅 🚩
kinyungu_denis
Kinyungu Denis

Posted on November 1, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related