Sub topic 11
Pinning the Environment
Prerequisites
This chapter assumes basic Python, command-line, and Git skills. You should also know how to install a machine learning library and run a training script.
A training result depends on more than your source code. It can change when Python, NumPy, a deep learning framework, or an operating-system library changes. An environment is the collection of these tools and their versions. Pinning an environment records those versions so another run uses the same inputs.
Why versions matter
Suppose your project uses scikit-learn==1.4.0 today. A later installation might select version 1.6.0. That release could change defaults, validation rules, or numerical behavior. A small change can alter metrics or even make old code fail.
Pin the versions that affect your result. Also record the Python version and, when relevant, the CUDA version used by a GPU framework. Keep this information in the repository rather than relying on one developer's machine.
A simple project may use a requirements file:
# requirements.txt
numpy==1.26.4
pandas==2.2.1
scikit-learn==1.4.1
pyyaml==6.0.1The == operator requests an exact version. A range such as numpy>=1.26 allows future upgrades, which is useful for libraries that are not part of the experiment's numerical path but less safe for core dependencies.
Create and record an environment
Use a virtual environment to isolate this project from other Python projects. The following commands create one, activate it, install the pinned dependencies, and record the interpreter version:
python3.11 -m venv .venv
. .venv/bin/activate
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
python --version
python -m pip freezeCommit requirements.txt, but usually do not commit .venv/. Add the environment directory to .gitignore:
.venv/
__pycache__/
*.pycpip freeze can help inspect the installed environment. It may include packages that your project did not directly request. For stronger reproducibility, teams often generate a lock file with a dependency-management tool. A lock file records the complete dependency tree, including transitive dependencies, which are packages installed by your direct dependencies.
What pinning does not solve
Pinned Python packages cannot guarantee identical results on every machine. Native libraries, CPU instruction sets, GPU drivers, and nondeterministic GPU operations can still differ. Record the operating system, hardware class, and framework settings when they matter.
Pinning also creates maintenance work. Old versions eventually receive security fixes or stop supporting newer Python releases. Separate routine dependency updates from experiment reproduction. Update deliberately, rerun tests, and record the new environment as a new project state.
Environment flow
Exercise
Why is a virtual environment useful when the dependency versions are already pinned?
Show answer
It prevents packages installed for another project from interfering with this project's pinned dependencies.