Explain Codes LogoExplain Codes Logo

How to run an .ipynb Jupyter Notebook from terminal?

python
jupyter
nbconvert
notebooks
Nikita BarsukovbyNikita Barsukov·Feb 20, 2025
TLDR

To run a Jupyter Notebook from the terminal, utilize this command:

jupyter nbconvert --execute --to notebook my_notebook.ipynb --output executed_notebook.ipynb

This one-liner runs my_notebook.ipynb and dumps the result in executed_notebook.ipynb. Don't forget you'll need the Jupyter suite and the trusty nbconvert tool installed.

In other words, this is the ctrl-alt-del of the Jupyter world; you'll get your notebook running without ever touching the mouse!

Running Notebooks in Terminal: A Deep Dive

The nbconvert Way

If the Jupyter GUI is not your cup of tea, nbconvert got your back! Run your notebook, convert it and even make coffee (well, not really, but wouldn't it be great?) using the classic command:

jupyter nbconvert --to <format> --execute <notebook>

You know the deal - swap <format> with notebook or python, and <notebook> with your file, like so:

jupyter nbconvert --execute --to python my_notebook.ipynb

That's it! Your notebook morphed into a Python script and ran faster than Bolt on the Olympics.

Making Changes Stick with --inplace

Want to keep the output in the same notebook? Say no more:

jupyter nbconvert --execute --to notebook --inplace your_notebook.ipynb

This hocus-pocus replaces the original notebook with the new executed one. Abracadabra!

Straight to the Point with IPython

IPython lets you score the goal without all the dribbling:

ipython --TerminalIPythonApp.file_to_run=my_notebook.ipynb

Or, you could stick to the classic move inside the IPython shell:

%run my_notebook.ipynb

And your notebook will run as if it was prom night!

Ring the Alias for Backup

Doing this too often? Go DRY! Set an alias in your .bashrc or equivalent:

alias run_notebook="jupyter nbconvert --execute --to notebook"

After this, just type run_notebook your_notebook.ipynb and you're golden!

Troubleshooting and Prerequisites

Before trying to run these power plays, get your ducks in a row. You might need mistune for nbconvert to play nice:

pip install -U mistune

More problems? Swing by the nbconvert GitHub repository for the latest gossip.

When .ipynb Becomes .py

Sometimes, your notebook needs a costume change. When integrating with a deeper workflow or setting up continuous integration services, transforming it into a .py can be a true-life saver. Use Jupyter's own tools or nbconvert to get this done quickly.

Tips, Tricks and Potential Pitfalls

Configuration Files for Smooth Sailing

To make your routine simpler, bake your commands into a jupyter_nbconvert_config.py. Define default conversion types and outputs in the configuration file, so you can keep your commands short and sweet.

Error Handling Like a Boss

Running non-interactive execution? You'll need a plan for when things go down. Use --ExecutePreprocessor.allow_errors=True to keep the party going even when you hit a snag.

Continuous Integration and Notebooks

Jupyter notebooks and CI pipelines go together like French fries and milkshakes! Convert your .ipynb to .py for easy integration with your CI tools.

Regular Execution with Cron

Combine nbconvert and cron for regular updates. If you're generating reports, working with live data, or just plain forgetful, this trick's for you!

Parameterized Execution with Papermill

Want to take your execution to the next level? Try papermill on for size! This tool lets you inject parameters and execute notebooks, ideal if you need to punch out multiple reports with different datasets.