Sub topic 6
Pinning and recording dependencies
This chapter assumes you can create a virtual environment, install packages with pip, and run a Jupyter notebook. It covers how to record the exact package versions an analysis used, so someone else can rebuild them.
Why an environment alone is not enough
A virtual environment keeps your project's packages separate from other projects. It does not record which versions you installed. If you run pip install pandas today and a colleague runs it in a year, you may get different versions. Different versions can give different numbers, different defaults, or errors.
Pinning means fixing a package to one exact version, such as pandas==2.2.2. Recording means saving those pins in a file that lives with the project.
Freezing an environment
The simplest approach is pip freeze, which lists every installed package with its exact version. Redirect the output to requirements.txt and commit the file.
python -m venv .venv
source .venv/bin/activate
pip install pandas matplotlib
pip freeze > requirements.txt
# Later, on another machine:
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtThe frozen file includes packages you did not install directly, such as NumPy, which pandas needs. These are transitive dependencies. Pinning them matters, because they can change results too.
Direct and pinned dependencies
A frozen file is exact but hard to read. Many projects keep two files:
| File | Contents | Purpose |
|---|---|---|
| `requirements.in` | Packages you chose, loosely versioned | Shows intent |
| `requirements.txt` | Every package, exactly pinned | Rebuilds the environment |
Tools such as pip-tools generate the second file from the first. If you use conda, conda env export --from-history plays a similar role for the packages you requested directly.
The Python version also counts
Package pins do not record the interpreter. Results can differ between Python 3.10 and 3.12, and some pinned versions will not install on a different Python at all. Note the Python version in your README, or in the notebook output as shown below.
Recording the environment inside the notebook
A requirements file can drift from what the notebook actually ran with. Printing versions in the notebook output ties the record to the results.
import platform
import sys
from importlib.metadata import version
print("Python:", sys.version)
print("Platform:", platform.platform())
for name in ["numpy", "pandas"]:
print(f"{name}:", version(name))Put this near the top of the notebook. Add every package whose behavior affects your results.
How the pieces fit
Common errors
- Freezing the wrong environment. The file lists hundreds of unrelated packages. Check that your prompt shows the virtual environment before you freeze.
- Forgetting to refresh the file. You install a new package but never re-run
pip freeze. A rebuild then fails with an import error. - Unpinned installs in the notebook. A cell that runs
!pip install pandaspulls whatever is newest. Install from the requirements file instead. - Platform-specific pins. A frozen file made on Windows may include packages that do not exist on Linux. If the team uses mixed systems, test the rebuild on each.
- Trusting the pins forever. Old pins can stop installing as the package index changes. Test a rebuild occasionally, and note the date you froze the file.
Check that the rebuild works
Pinning is only useful if the rebuild succeeds. Create a fresh environment in a temporary folder, install from requirements.txt, and run the notebook with Restart and Run All. If it fails, your record is incomplete.
Exercise
You run pip freeze > requirements.txt and later notice the file lists packages unrelated to your project. What most likely went wrong, and how do you fix it?
Show answer
You ran the command without the project's virtual environment activated, so pip listed the global packages. Activate .venv, confirm the prompt shows it, and run the freeze command again.