How to run an .ipynb Jupyter Notebook from terminal?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
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.
Related reading
- How to sample large database and implement K-means and K-nn in R?
- How to save a list of numpy arrays into a single file and load file back to original form
- How to save a Python interactive session?
- How to save a Seaborn plot into a file
- How to run Anaconda Python on sudo
- How to run Django's test database only in memory?
- How to save Amazon Redshift output to local CSV through SQL Workbench?
- How to save one hot encoder?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.