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 .venvCommon 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 pytestOptions
| Option | Does |
|---|---|
| --upgrade-deps | Upgrade pip/setuptools in the new env |
| --system-site-packages | Give the venv access to system packages |
| --clear | Delete contents of an existing env dir |
| --prompt NAME | Set the activation prompt label |
Related guides
pip install: Usage, Options & Common CI ErrorsHow pip install works - installing packages, version specifiers, and the options you reach for, plus the CI f…
python -m pip: Run pip for the Right InterpreterWhy python -m pip is safer than a bare pip in CI, how it guarantees you install into the intended interpreter…
uv venv: Create a Virtual Environment FastHow uv venv creates a virtual environment quickly, how to pin the Python version with --python, and how it pa…