Skip to content
Latchkey

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 pytest

In 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →