LinuxCommandLibrary

venv

Create isolated Python environments

TLDR

Create a Python virtual environment

$ python -m venv [path/to/virtual_environment]
copy

Activate the virtual environment (Linux and macOS)
$ source [path/to/virtual_environment]/bin/activate
copy

Activate the virtual environment (Windows)
$ [path\to\virtual_environment]\Scripts\activate.bat
copy

Deactivate the virtual environment
$ deactivate
copy

Create an alias that generates a venv folder and automatically activates it
$ alias venv='python -m venv .venv && source [.venv/bin/activate|.venv\Scripts\activate.bat]'
copy

SYNOPSIS

python3 -m venv [options]

PARAMETERS

-h, --help
    Show help message and exit.

--system-site-packages
    Give the virtual environment access to the system site-packages directory. By default, environments are isolated.

--symlinks
    Try to symlink rather than copy files. This is often the default on macOS and Windows (Python 3.9+).

--copies
    Try to copy rather than symlink files. Use this if symlinking causes issues.

--clear
    Delete the contents of the environment directory if it already exists, then recreate the environment.

--upgrade-deps
    Upgrade pip, setuptools, and wheel to the latest versions in the created virtual environment (Python 3.9+).

--without-pip
    Skips installing pip, setuptools, and wheel into the new virtual environment.

DESCRIPTION

The venv module, bundled with Python 3.3 and later, provides a lightweight way to create isolated Python environments. It's not a standalone Linux executable but a module invoked via the Python interpreter: python3 -m venv .

These virtual environments allow developers to manage project-specific dependencies without interfering with the system-wide Python installation or other projects. Each environment has its own Python interpreter, pip, and an independent set of installed packages. This isolation is crucial for reproducibility and preventing dependency conflicts. Once created, an environment is activated, which modifies the shell's PATH to prioritize the environment's executables, making it seamless to work within the isolated context. Deactivation returns the shell to its previous state. venv is the modern, official approach for virtual environments in Python.

CAVEATS

The venv module requires Python 3.3 or later. For Python 2 projects, the external virtualenv tool is typically used. Remember to explicitly activate the environment after creation to use it, and to install packages within it using pip.

INVOCATION METHOD

venv is not a standalone executable command. It is invoked as a Python module using the Python interpreter: python3 -m venv . This distinction is important as it means you must have a Python 3 interpreter installed to use it.

ENVIRONMENT ACTIVATION

After creating a virtual environment, it must be activated to modify your shell's environment variables (like PATH) to point to the environment's Python interpreter and scripts. The activation command varies by shell and operating system:
Bash/Zsh: source /bin/activate
Windows Command Prompt: \Scripts\activate.bat
PowerShell: \Scripts\Activate.ps1
To exit an activated environment, simply run deactivate.

HISTORY

The venv module was introduced as a built-in feature in Python 3.3 (released 2012) to provide a standardized, official way to manage virtual environments. Prior to its inclusion, the third-party virtualenv tool was the de-facto standard. venv aims to simplify environment management by integrating it directly into the Python standard library, making it more accessible and reducing external dependencies for basic use cases.

SEE ALSO

python3(1), pip(1), virtualenv(1)

Copied to clipboard