python -m venv: Virtual Environment Command Reference
Create the isolated environment every Python CI job should install into.
python -m venv creates a self-contained environment with its own interpreter and site-packages. Installing into a venv avoids the system-Python PEP 668 "externally-managed-environment" error.
Common flags / usage
- python -m venv .venv -- create an environment in .venv
- --upgrade-deps -- upgrade pip/setuptools in the new env
- --system-site-packages -- let the venv see system packages
- --clear -- wipe an existing target directory first
- --prompt NAME -- set the activation prompt label
Example
shell
- run: python -m venv .venv
# Call binaries by path so activation is not needed across steps:
- run: .venv/bin/pip install -r requirements.txt
- run: .venv/bin/python -m pytestIn CI
Each CI step often runs in a fresh shell, so activating a venv in one step does not carry to the next. Either call the venv binaries by path (.venv/bin/python) or re-activate in every step.
Key takeaways
- python -m venv creates an isolated interpreter and package set.
- Installing into a venv avoids the PEP 668 system-Python error.
- Activation does not persist across steps -- call binaries by path instead.
Related guides
source venv/bin/activate: Activation Command ReferenceReference for activating a virtualenv in CI: source venv/bin/activate on Linux/macOS, the Windows path, and w…
python -m pip: Invoke pip Safely Command ReferenceReference for python -m pip in CI: running pip for a specific interpreter to avoid the wrong-pip-on-PATH prob…
uv venv: Fast Virtual Environment Command ReferenceReference for uv venv in CI: creating a virtual environment quickly, pinning the Python version with --python…