Explain Codes LogoExplain Codes Logo

How do I convert an IPython Notebook into a Python file via commandline?

python
jupyter
nbconvert
command-line
Anton ShumikhinbyAnton Shumikhin·Jan 11, 2025
TLDR

Converting an IPython Notebook into a Python file is simple:

jupyter nbconvert --to python <YourNotebook>.ipynb

This results in a Python file <YourNotebook>.py, containing the executable code from the notebook.

Intermediate functionalities

Batch conversion

For those tired of the one-by-one ritual, enjoy the beauty of wildcard *.ipynb:

jupyter nbconvert --to script *.ipynb

Transforms all notebooks in the directory to Python scripts in one command—wow!

In-notebook command execution

Life-hack: Run your conversion without leaving the mother ship Notebook:

!jupyter nbconvert --to script <YourNotebook>.ipynb

This is the equivalent of "Go to Shell -> Do the Thing -> Return to Python." Neat, right?

Simplifying commands

Tired of typing? Wrap the conversion command in a handy script:

echo '#!/bin/bash' > convert_notebooks.sh echo 'jupyter nbconvert --to script "$@"' >> convert_notebooks.sh chmod +x convert_notebooks.sh ./convert_notebooks.sh <YourNotebook>.ipynb

This writes a script convert_notebooks.sh that you can use and reuse―don't forget to replace <YourNotebook>.ipynb!

Double-sided magic with Jupytext

Jupytext enables bidirectional conversion between the .ipynb and .py formats:

jupytext --to py notebook.ipynb

Like a magician with doves, but with code files instead.

Nitty-gritty details

Decoding command-line syntax

The --to script argument does the heavy lifting. This is the newer alternative to --to python.

Going beyond the basics

Leverage the nbconvert API directly in Python:

from nbconvert import PythonExporter import nbformat notebook_filename = '<YourNotebook>.ipynb' with open(notebook_filename) as fh: nb = nbformat.reads(fh.read(), as_version=4) exporter = PythonExporter() source, meta = exporter.from_notebook_node(nb) with open('<YourScript>.py', 'w+') as fh: fh.writelines(source)

This sequence of commands might look like Harry Potter's spell list, but each does something powerful.

Script execution with Jupytext

Turn static code into living, breathing scripts:

jupytext --to notebook --execute <YourNotebook>.py

Now your Python script runs & updates your Jupyter Notebook. It's a programming inception!

Advance use cases

Compatibility issues

When extracting notebook code, watch out for compatibility with nbformat versions. Like dealing with teenagers, different versions require different approaches!

Automation without GUI

Scripts make it easy to automate conversions on non-GUI platforms―like a butler for your headless server or Docker container.

Task scheduling

Script conversion opens the door to Cron jobs and task automation—so you can set it, forget it, and let your code carry the burden!

Complete conversion

Manual JSON parsing can miss metadata—it's like asking a five-year old to proofread your resume. Use dedicated libraries instead!