Use this when isolating Python dependencies.
Virtual Environments
Virtual environments (python -m venv .venv) isolate a project's Python packages from the system and other projects, so each automation has the exact dependencies it needs.
1. Virtual environments isolate dependencies per project.
2. Never install into system Python.
3. Commit lock files, not the venv.
4. Recreate in CI.
5. uv/poetry as alternatives.
project → venv → isolated packages
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Verification path Verify boundary
The negative fixture should deliberately exercise risks such as environment drift, implicit coercion, broad exception handling, blocking/async confusion, shell quoting/subprocess risks, partial writes and non-deterministic external APIs, choosing the cases most relevant to venv interpreter/path identity, dependency isolation, activation-independent invocation and reproducible package/version lock for noninteractive runs.
Step 2 Example Narrow the fault
python3 -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -r requirements.txt
Step 3 Common mistakes Narrow the fault
- Committing the
.venvfolder to Git. - Forgetting to activate it in cron/CI jobs.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- python -m venv .venv creates an isolated environment.
- Activate before installing packages.
- One venv per project.
python3 -m venv .venv— create. .venv/bin/activate && which python— active envpip freeze > requirements.txt— capture deps
ReferenceWhy it matters1 section · depth on demand
What it is
A virtual environment is an isolated Python install per project, so dependency versions do not clash with the OS or other projects.
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
A virtual environment is an isolated Python installation for one project, with its own packages and versions. It prevents conflicts between projects and with the system Python, and makes dependencies explicit.
python3 -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\Activate.ps1
pip install -r requirements.txt
which python # confirm the venv is active
deactivate
Do not commit .venv/ to Git — commit requirements.txt (or pyproject.toml with a lock file) instead. Tools such as uv or Poetry create and sync environments faster and record exact versions. In cron jobs and systemd units, call the venv's interpreter directly (/opt/tool/.venv/bin/python script.py), since activation scripts do not run there.
Mechanics
The linked authority for bounded mechanics is Python Documentation — venv; documentation support is separate from observed lab/production Evidence.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Python Automation > Virtual Environments. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://docs.python.org/3/library/venv.html · confidence 94%Deep reference
Healthy vs suspicious evidence
Output interpretation
For venv interpreter/path identity, dependency isolation, activation-independent invocation and reproducible package/version lock for noninteractive runs, prefer typed fields and stable IDs over human presentation text.