A Python virtual environment is an environment isolated from your system, which allows you to run packages for a project separately from your system’s packages, so that you only use the packages that will be useful in your project.

In this mini-tutorial, we will learn more about Python virtual environments, how they work, and most importantly how to create and configure a virtual environment.

How does a Python virtual environment work?

Normally Python programs use all the packages and modules that are installed in your system, even if only a few of them are used by the program; the logic of virtual environments is to limit the use of packages to those that are used in the Python program.

Norton Secure VPN 2023 for up to 5 Devices
19,99 $

What modules are used to create virtual environments?


In Python, there are several modules for creating virtual environments, the most well known of which are: virtualenv, Pipenv, Conda, venv which is usually delivered with Python. In this tutorial we will use venv, but any of these modules will do.

Requirements for creating a Python virtual environment

The following tools are essential for the rest of the tutorial:

  • The Python interpreter installed globally
  • A minimum of knowledge in using a command prompt
  • A properly functioning brain

Let’s move on to the most important part of the tutorial.

How to create a Python virtual environment with the “venv” module?

The creation of a virtual environment depends on the operating system you are using; To create a Python virtual environment with the venv module, 03 cases have to be taken into account

       If you are using Windows as operating system

In this case, you just have to :

  • Open the command prompt
  • Move to the directory from which you want to create the virtual environment
  • Type the command “python -m venv environment_name” and you’re done.

If you use Linux as your operating system


If you are using Linux as your operating system, after opening your command prompt and moving to the directory where you want to create the virtual environment, simply type the command: “python -m venv environment_name”.

Python3 as the current version of Python is version 3.

Creating virtual environments is all well and good, but in order to use them, you need to activate them. What do you need to do to activate a Python virtual environment?

How do you activate and deactivate your virtual environment?

     Activating a virtual environment


As you may have guessed, activating a virtual environment also depends on the operating system used:

– On Windows, simply enter the following command in the “Scripts” child directory where your virtual environment is located: “activate.bat”

On the other hand, if you are on Linux, enter this command “activate” in the child directory

         Deactivating a virtual environment

To do this, go to the root of your virtual environment and type the command “deactivate”.

You now know how to create and configure a Python virtual environment with the venv module.


Find out :

Leave a Reply

Your email address will not be published. Required fields are marked *

20 − two =