Categories: Web tips and tricks

All about virtual environments in Python

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 :

Partager

Post Récent

How to Fix the Windows 11 Update Error

The Windows 11 update error can occur for several reasons, such as corrupted files, connectivity issues, or conflicts with third-party…

il y'a 3 months

How to resolve the error “You don’t currently have permission to access this folder” in Windows 10 and 11

The "You don’t currently have permission to access this folder" error often appears when you try to access a folder…

il y'a 3 months

How to Fix the Flutter Error ‘This version only understands SDK XML versions up to 3 but an SDK XML file of version 4 was encountered’

When developing Flutter applications, it's common to encounter errors related to SDK version compatibility. One of these common errors is…

il y'a 4 months

Windows cannot be installed on this disk: the hardware of this computer might not support booting from this disk

During the installation of Windows, you may encounter an error indicating that the selected disk is not compatible with the…

il y'a 5 months

Windows could not start the Windows Update service on the local computer

The error "Windows could not start the Windows Update service on the local computer" occurs when the operating system fails…

il y'a 5 months

The Missing Windows Update Service on Windows 10 or 11

The Windows Update Service is essential for keeping your system up to date with the latest security and performance improvements.…

il y'a 5 months