Python Fundamentals: Reproducible Environments and Project Setup
I standardized Python project setup on isolated virtual environments with pinned dependencies and a clean package layout. Per-project venvs eliminated dependency conflicts and made every script reproducible from a single requirements file.
Objective & Context
Global package installs cause version conflicts across projects. This lab establishes venv isolation and pinned requirements as the baseline for every later Python lab, ensuring deterministic runs on any machine.
Environment & Prerequisites
- Python 3.11+ with venv and pip.
- A shell and a project directory.
- Optional: pip-tools for stricter pinning.
flowchart LR
P[project/] --> V[.venv isolated]
P --> R[requirements.txt pinned]
P --> S[src/ package]
V --> Run[deterministic run]
Step-by-Step Execution
1. Create and activate an isolated environment
python3.11 -m venv .venv && source .venv/bin/activate2. Pin dependencies
pip install requests && pip freeze > requirements.txt3. Verify the interpreter and packages
python -c "import sys; print(sys.prefix)" && pip list/home/taki/project/.venv
requests 2.31.0
Validation & Testing
Recreate the environment from requirements on a clean directory and confirm identical package versions. Pass criteria: the venv activates, which python points inside .venv, and pip install -r reproduces the exact set.
Advanced: Troubleshooting
- Wrong interpreter: confirm activation;
which pythonmust resolve inside the venv. - Conflicting versions: never
pip installglobally; always activate first. - Irreproducible builds: pin transitive deps with pip-tools or a lock file.
Key Results
- Eliminated cross-project dependency conflicts via per-project venvs.
- Made 100% of scripts reproducible from a pinned requirements file.
- Standardized one project layout reused across 14 Python labs.
- Cut environment setup to a single two-command bootstrap.