Python Venv

Python virtual environments are the recommended manner for managing dependencies and developing Python programs. They are managed by the standard library's venv module.

See upstream documentation here and an official tutorial here.


Creation

If creating a new virtual environment, use the following command.

python3 -m venv path/to/virtual/environment

This will create a new directory containing the environment. A common target is .venv nested within a project directory.


Usage

To use a virtual environment, it has to be sourced. For most operating systems, use:

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

On Windows, execute path\to\virtual\environment\Scripts\activate.bat.

Now any pip(1)-installed packages will be restricted to this virtual environment. If setting up a local copy of an existing project, this one line command should accomplish installation:

python -m pip install -r requirements.txt


See also

Python Module of the Day article for venv


CategoryRicottone

Python/Venv (last edited 2023-06-15 18:41:33 by DominicRicottone)