Sub topic 12
Running notebooks from the command line
This chapter assumes you can write a Jupyter notebook, use a terminal, and have a virtual environment with pinned dependencies. Running a notebook without a person clicking through it removes one source of drift: the order and timing of manual execution.
Why run headless
A notebook opened in a browser carries whatever state your kernel built up during the session. A command-line run starts a fresh kernel and executes every cell from top to bottom. If the result differs from your interactive session, you have found hidden state.
Headless execution is also what automation needs. A scheduled job or a CI check cannot click "Run all".
Executing with nbconvert
The nbconvert tool ships with Jupyter. It runs every cell in order in a new kernel and saves an executed copy.
jupyter nbconvert --to notebook --execute analysis.ipynb --output analysis_executed.ipynbThe command fails with a non-zero exit code if any cell raises an error. That makes it usable in shell scripts and CI. The original analysis.ipynb is left unchanged, and the executed copy holds the outputs.
Run the command from the environment you want to test. Activate the virtual environment first, or the kernel may use a different Python than you expect.
Parameterizing with Papermill
Papermill runs a notebook with different inputs. You mark one cell with the tag parameters and define default values there.
alpha = 0.05
input_path = "data/raw/measurements.csv"Papermill injects a new cell after the tagged one. That cell holds the values you pass on the command line, so they override the defaults.
papermill analysis.ipynb outputs/analysis_alpha_0.1.ipynb -p alpha 0.1Each run writes its own output notebook. Name the files after the parameters, as above, so you can tell the runs apart later.
Papermill gives parameters the types it can infer from the command line. -p alpha 0.1 becomes a float. Check types inside the notebook if a value could be read as the wrong kind, such as a string that looks like a number.
What a run looks like
The diagram shows the flow from source notebook to saved results.
Common errors
- Relative paths break. Headless runs may start in a different working directory. Use paths relative to the project root, or set the working directory explicitly in the command.
- Missing kernel. The kernel named in the notebook is not installed in the active environment. Install it or pick another with
--ExecutePreprocessor.kernel_name. - Long cells time out.
nbconvertstops a cell after 30 seconds by default in some setups. Pass--ExecutePreprocessor.timeout=600to raise the limit, or-1for no limit. - Interactive input. Cells that call
input()or open a plot window wait forever. Remove them from notebooks meant to run unattended. - Overwriting the source. Always write to a separate output file so you keep the clean original.
Choosing a tool
| Need | Tool |
|---|---|
| Run once and check it works | `jupyter nbconvert --execute` |
| Run many times with different inputs | `papermill` |
| Keep each run's executed copy | Either, with a distinct `--output` name |
Exercise
You run papermill analysis.ipynb out.ipynb -p alpha 0.1, but the results look identical to a run with the default alpha. Name two likely causes in the notebook.
Show answer
First, the cell that defines alpha may not be tagged parameters, so Papermill has nowhere to inject the override. Second, a later cell may reassign alpha and overwrite the injected value. Check the cell tag and search the notebook for other assignments to alpha.