Python Virtual Env
Tutorial 63 of 65 · pythondeck.com Python course
A virtual environment is a self-contained Python installation living in a folder. It isolates project dependencies and avoids polluting the system interpreter. The standard tool is the venv module.
Virtual environments isolate project dependencies from system Python—reproducible installs, no sudo pip, and fewer conflicts between client projects on one laptop.
venv is built-in; tools like uv and poetry build on the same principle for modern workflows.
Onboarding a teammate starts with the same interpreter and dependency tree—virtual envs are the cheapest reproducibility win before Docker.
venv module — python -m venv .venv; activate per shell/OS.
pip in venv — installs only into env; never mix with global pip.
requirements.txt — pinned versions from pip freeze or pip-tools compile.
pyenv / asdf — multiple Python versions side by side.
containers — Docker as stronger isolation for deploy parity.
PEP 668 — externally managed base Python on some distros blocks global pip.
Treat .venv as disposable: document Python version in .python-version or pyproject requires-python. CI should create fresh venv each run. For apps, lock with hashes (pip install --require-hashes) when security demands supply-chain rigor.
Editors should point to .venv interpreter for IntelliSense and debugging alignment.
uv and pip-tools can compile lockfiles from pyproject.toml—faster installs without sacrificing pin discipline.
Name the venv `.venv` consistently so IDEs and CI scripts find the interpreter without custom paths.
Committing site-packages or entire .venv to git.
Installing packages globally on PEP 668 systems and breaking OS tools.
Unpinned requirements causing CI pass locally, fail tomorrow.
Different Python minor version between dev and production containers.
Add .venv to .gitignore; one venv per repo root convention.
Regenerate lockfiles when upgrading major dependencies.
Use python -m pip not bare pip to ensure correct interpreter.
Document setup in README: create venv, install, run tests.
Pin build tools (pip, setuptools) in CI to catch packaging regressions early.
Re-read the examples below with these ideas in mind; change variable names and inputs to match your own project.
The program below demonstrates create + activate. Read the comments on each line, run the code, then change names or values to see how the output shifts.
# Example: Create + activate
# Run in the REPL or save as a .py file and execute with python.
python -m venv .venv
# Windows:
.venv\Scripts\activate
# macOS / Linux:
source .venv/bin/activate
python -m pip install --upgrade pip
This sample walks through install + freeze in a small, runnable script. Paste it into the REPL or save it as a .py file before you continue to the next block.
# Example: Install + freeze
# Run in the REPL or save as a .py file and execute with python.
pip install requests flask
pip freeze > requirements.txt
Here is a hands-on illustration of recreate. Follow the inline comments first; only then execute the snippet and compare the result with what you expected.
# Example: Recreate
# Run in the REPL or save as a .py file and execute with python.
deactivate
rm -rf .venv
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
The program below demonstrates create activate. Read the comments on each line, run the code, then change names or values to see how the output shifts.
# venv copies base interpreter into an isolated prefix
python -m venv .venv # create environment directory
# Windows activation:
.venv\Scripts\activate # cmd/powershell
# Unix activation:
source .venv/bin/activate # bash/zsh
python -c "import sys; print(sys.prefix)" # should point inside .venv
python -m pip install --upgrade pip # fresh pip in env
pip install httpx # example dependency
pip freeze # list pinned packages
deactivate # leave virtual environment
This sample walks through requirements sync in a small, runnable script. Paste it into the REPL or save it as a .py file before you continue to the next block.
# Pin dependencies for reproducible installs
python -m venv .venv # ensure env exists
source .venv/bin/activate # Unix — adjust on Windows
pip install -r requirements.txt # install pins
pip check # verify no broken requirements
pip list --outdated # see upgradable packages
pip install -U package_name # controlled upgrade
pip freeze > requirements.lock # snapshot after upgrade
deactivate # done