Sub topic 11
Make the notebook reproducible by recording dependencies
A notebook that runs on your machine but fails on a colleague's machine is not reproducible. The usual cause is a different set of installed packages. This chapter shows how to record your dependencies so someone else can rebuild your environment.
This chapter assumes you have already created a virtual environment, installed JupyterLab, pandas, and matplotlib, and registered a kernel. It also assumes you know why a notebook can carry hidden state.
Why versions matter
Libraries change between releases. A function can gain a new argument, change a default, or be removed. If your notebook ran with pandas 2.2 and a colleague installs a different major version, the same code can give different results or raise an error.
Recording versions means writing down exactly what your environment contains. Then anyone can install the same set.
Freeze the environment
With the virtual environment active, run:
python -m pip freeze > requirements.txtpip freeze lists every installed package with its exact version. The > operator writes that list to requirements.txt. The file looks like this:
jupyterlab==4.2.5
matplotlib==3.9.2
pandas==2.2.3Your file will be longer. It includes the dependencies of those packages too, such as numpy, and the versions will differ from the example. Commit the file next to your notebook.
Rebuild the environment elsewhere
Another person recreates your setup in four steps:
- Clone or copy the project folder.
- Create a new virtual environment with
python -m venv .venv. - Activate it.
- Run
python -m pip install -r requirements.txt.
They should then register a kernel, as you did earlier, and select it in the notebook.
Common errors
- Freezing the wrong environment. If the virtual environment is not active,
pip freezelists your global packages. Check that your prompt shows(.venv), or runpython -m pip --versionand confirm the path points inside.venv. - Using a bare
pip. Plainpipmay belong to a different Python than the one you intend. Usepython -m pipso the package manager matches the interpreter. - Letting the file go stale. If you install a new library later, the file does not update itself. Run the freeze command again before you commit.
- A kernel that points elsewhere. A correct
requirements.txtdoes not help if the notebook uses a kernel from another environment. Check the kernel name in the top right of the notebook. - Platform differences. A frozen file from one operating system can contain packages that do not exist on another. If a collaborator uses a different OS, expect to edit the file.
Record the Python version too
Package versions are only half of the picture. The Python version also matters. Note it in a README, for example "Tested with Python 3.12". You can print it from inside the notebook:
import sys
import pandas as pd
import matplotlib
print(sys.version)
print("pandas", pd.__version__)
print("matplotlib", matplotlib.__version__)Put this in the first cell. Each time someone runs the notebook, the output shows which versions produced the results.
Combine with clean execution
Dependencies are one part of reproducibility. The other part is execution order. Before you share a notebook, restart the kernel and run all cells from top to bottom. If this succeeds in a fresh environment built from requirements.txt, the notebook is in good shape.
Exercise
A collaborator installs your packages from requirements.txt, but your notebook still raises an ImportError for pandas when they open it. The install finished without errors. What is the most likely cause, and how do you check it?
Show answer
The notebook is probably using a kernel from a different environment, one that does not have pandas installed. Check the kernel name shown in the top right of the notebook and switch to the kernel registered from the new virtual environment. You can also run import sys; print(sys.executable) in a cell to see which Python the kernel uses. The path should point inside the project's .venv.
Exercise
You install the seaborn library into your active virtual environment a week after creating requirements.txt. What should you do before committing your work, and why?
Show answer
Run python -m pip freeze > requirements.txt again. The file does not update automatically, so without this step a collaborator would not get seaborn and the notebook would fail on their machine.