Sub topic 3
Restart and run all before you trust a result
This chapter assumes you can write and run Jupyter notebook cells in Python and use a terminal. It builds on the previous chapter, where hidden state was the cause of results that cannot be repeated. Here you turn the fix into a routine check.
The check
A notebook keeps variables alive in a running kernel. The kernel is the Python process behind the notebook. A cell that worked yesterday may depend on a variable you defined in a cell you later deleted or edited. The only way to find out is to throw the kernel away and run everything again from the top.
The check has two steps:
- Restart the kernel. This clears all variables, imports, and loaded data.
- Run all cells in order, from top to bottom.
In JupyterLab, choose Kernel > Restart Kernel and Run All Cells. In classic Notebook, choose Kernel > Restart & Run All. If every cell finishes without an error and the results look the same as before, the notebook is clean.
What a failure tells you
A failed clean run is useful. The error points at a real dependency that was never written down in the notebook. These are the most common causes.
| Symptom after restart | Likely cause | Fix |
|---|---|---|
| `NameError: name 'df_clean' is not defined` | The cell that creates `df_clean` was deleted, moved below its first use, or never run | Put the defining cell above the cells that use it |
| `ModuleNotFoundError` | The package was installed in a different environment than the kernel uses | Install it in the kernel's environment and record it (see later chapters) |
| Numbers differ from the last run | A random process has no seed, or a cell was edited after its output was produced | Seed the generator and re-run |
| `FileNotFoundError` | A path was relative to a folder you had changed in an earlier session | Build paths from a known project root |
Notice that the last two do not always raise an error. A silent change in a number is harder to catch than a crash, so compare outputs, not only whether the run finished.
A quick check you can run in a cell
You can also guard against out-of-order work inside the notebook. Execution counts are the numbers shown as In [n] beside each cell. After a clean run they should read 1, 2, 3 and so on with no gaps. If they jump around, the saved outputs came from a different order than the cells appear on the page.
Doing it from the command line
The same check can run without opening a browser. This command runs every cell in order in a fresh kernel and saves the executed copy. It fails if any cell raises an error.
jupyter nbconvert --to notebook --execute analysis.ipynb --output analysis_executed.ipynbBecause it always starts a new kernel, it is a stricter test than clicking through the menu while you still have other windows and variables open. A later chapter covers running notebooks this way in more depth and in automated jobs.
When to run the check
- Before you share a notebook or commit it.
- Before you copy a number or a figure into a report.
- After you reorder, delete, or merge cells.
- After you install or upgrade a package.
Exercise
You delete a cell that defined threshold = 0.5. Later cells still use threshold, and the notebook keeps working. Why does it still work, and how do you find the problem?
Show answer
The kernel still holds threshold in memory from when the deleted cell ran. Restart the kernel and run all cells in order. The first cell that uses threshold then raises a NameError, which shows that the notebook no longer defines it. Restore the definition in a cell above its first use.
Exercise
A notebook finishes "Restart and run all" with no errors, but the mean printed in the last cell differs from the previous run. Name one likely cause and one way to confirm it.
Show answer
A likely cause is an unseeded random number generator. To confirm, run the clean-run check twice and see whether the number changes each time. If it does, create a generator from a fixed seed, for example np.random.default_rng(20240601), and use it for all random draws.