"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Python virtual Environments

Python virtual Environments

Published on 2024-08-01
Browse:135

Python virtual Environments

TL;DR

Essentially this allows you to create an isolated environment for each python application you create. Meaning each application can use difference libraries or even different version of the same library without interfering with each other.

What is VENV

A python virtual environment or VENV is a lightweight self-contained directory tree that contains a Python installation for a particular version of python, plus a number of additional packages.

Each python application you create can use its own virtual environment. This resolves the problem of conflicting requirements between applications.

The venv module is used to create the virtual environment.

How to install venv

pip install virtualenv

Create a virtual environment

python -m venv /path/to/new/virtual/environment

Use a virtual environment

For Linux or Mac OS your new venv can be activated by using this command:

source /path/to/new/virtual/environment/bin/activate

For windows:

\path\to\new\virtual\environment\Scripts\activate.bat

Once activated you should be able to install libraries via pip like you normally would but it will now be installed into you virtual environment

Exiting your virtual environment

Just like with activation venv provides a handy deactivate script. For Linux or Mac OS your new venv can be deactivated by using this command:

deactivate

or if that does not work:

source /path/to/new/virtual/environment/bin/deactivate

For windows:

\path\to\new\virtual\environment\Scripts\deactivate.bat

Why should you use a virtual environment

When you run Python and its libraries from the system, you are restricted to a specific Python version chosen by your operating system. This approach can lead to version conflicts among the libraries when running multiple Python applications on the same installation. Additionally, modifying the system Python may disrupt other OS features that rely on it. Using a virtual environment isolates each application thus solving this problem.

Release Statement This article is reproduced at: https://dev.to/naicigam28/python-virtual-environments-pl2?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3