venv
Create isolated Python environments
TLDR
Create a Python virtual environment
Activate the virtual environment (Linux and macOS)
Activate the virtual environment (Windows)
Deactivate the virtual environment
Create an alias that generates a venv folder and automatically activates it
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
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
Windows Command Prompt:
PowerShell:
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)