How to run an .ipynb Jupyter Notebook from terminal?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Running a Jupyter notebook from the terminal is useful when you want notebook execution without opening the browser UI. The most common solution is jupyter nbconvert --execute, while tools such as papermill are better when the notebook needs parameters or repeatable automation.
The Standard Command: nbconvert --execute
For a normal notebook, the simplest command is:
This runs the notebook and writes an executed notebook file in the output location chosen by nbconvert. If you want to overwrite the original file with the executed outputs, add --inplace:
This is the usual choice for scheduled jobs, CI, and remote servers where you still want a real notebook file as the result.
Timeouts and Working Directory
Two options matter more than most people expect:
- timeout for long-running cells
- the working directory used during execution
A notebook that reads files with relative paths can fail if it is executed from the wrong directory. A notebook with heavy computation can fail if the default timeout is too short.
If a cell may run for a very long time, you can disable the timeout entirely:
Use papermill When the Notebook Needs Parameters
If the notebook should run with different input values each time, papermill is usually the better tool.
Inside the notebook, create a parameters cell with defaults:
papermill replaces those values during execution and saves the result into a separate output notebook. This is a strong fit for data pipelines, reporting jobs, and experiments that need repeatable configuration.
Running Through Python Code
If you need notebook execution as part of a Python application rather than a shell script, use nbformat with ExecutePreprocessor.
This is useful when notebook execution is one step inside a larger Python-driven workflow.
Converting to a Script Instead
Sometimes the real goal is not to preserve the notebook format at all. In that case, convert the notebook to a script and run the script directly.
That can be a good debugging aid because the generated script is easier to inspect in stack traces and command-line logs. The tradeoff is that notebook-specific outputs and metadata are no longer the main execution artifact.
Common Pitfalls
- Forgetting that relative file paths depend on the execution working directory.
- Leaving the default timeout in place for notebooks with long-running cells.
- Running the command in the wrong virtual environment or conda environment.
- Expecting UI-specific behavior from a headless terminal run, especially for plotting backends.
- Using raw shell execution when the notebook really needs parameters and
papermillwould be clearer.
Summary
- Use
jupyter nbconvert --to notebook --executefor the normal terminal-based notebook run. - Add
--inplaceif you want to overwrite the original notebook with executed output. - Set timeout and working directory explicitly for reliable automation.
- Use
papermillwhen notebook parameters should change between runs. - Use the Python API when notebook execution is part of a larger program.

