Skip to content
Latchkey

python -m venv: Create Virtual Environments

Create the isolated environment every Python CI job should install into.

python -m venv creates a self-contained virtual environment with its own interpreter and site-packages. Installing into a venv avoids the system-Python PEP 668 errors.

What it does

Creates a directory containing a Python interpreter and an isolated package set. After activation, "python" and "pip" resolve to the venv, keeping the system Python untouched.

Common usage

Terminal
python -m venv .venv

# Linux/macOS
. .venv/bin/activate

# Windows (PowerShell)
.\.venv\Scripts\Activate.ps1

python -m venv --upgrade-deps .venv

Common CI gotcha: activation lost between steps

Each CI step often runs in a fresh shell, so "activate" in one step does not carry to the next. Call the venv’s binaries by path, or re-activate in each step.

Terminal
# Re-activation not guaranteed across steps. Call by path:
.venv/bin/pip install -r requirements.txt
.venv/bin/python -m pytest

Options

OptionDoes
--upgrade-depsUpgrade pip/setuptools in the new env
--system-site-packagesGive the venv access to system packages
--clearDelete contents of an existing env dir
--prompt NAMESet the activation prompt label

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →