Written and maintained by CASRAI Editorial Board
Published
On this page
To convert a Jupyter notebook to a Python script, run jupyter nbconvert --to script notebook.ipynb in a terminal, or use the export option in the JupyterLab File menu. Both routes use the nbconvert tool and write a plain-text script with the notebook’s code cells in order. This guide covers the command, its output controls, the JupyterLab route, the Jupytext alternative that keeps a notebook and a script in sync, and what to check when the conversion does not behave as expected.
What nbconvert is used for in research
nbconvert is the Project Jupyter tool that turns a notebook (an .ipynb file) into another format. Its documentation lists HTML, LaTeX, PDF, WebPDF, Reveal.js slides, Markdown, AsciiDoc, reStructuredText, an executable script and a notebook as output formats. For analysis work, the script format is the one that moves a notebook out of an interactive session: a plain-text file can be run non-interactively, kept under version control as readable text, and called from a larger pipeline.
Notebooks suit exploration, where a researcher runs a cell, inspects a result and adjusts. Scripts suit the stages that follow, such as re-running an analysis on a new data release or handing the code to a collaborator who works from the command line. Converting does not change what the code does; it changes the container the code lives in. Cell outputs such as figures are not part of a script.
Two related exporters matter here. The nbconvert exporter reference describes the PythonExporter as one that “exports a Python code file”, and the output carries the shebang line #!/usr/bin/env python regardless of which Python version the notebook was written for. The script format is the general “Executable script” option: the usage documentation shows it converting a Julia notebook with the same syntax that works for any notebook. For a Python notebook, the Python exporter and the script format both target a Python file. The usage page documents --to script; python is listed among the built-in format names in the configuration reference.
How to install or access nbconvert
nbconvert is installed with pip or conda, according to its installation page:
pip install nbconvert- or, in a conda environment,
conda install nbconvert
The installation page states that nbconvert supports and tests Python 3.11 to 3.14. Converting to HTML needs nothing further, but formats other than HTML need Pandoc (version 1.12.1 or later), PDF export through LaTeX needs a TeX installation with XeLaTeX, and WebPDF needs the nbconvert[webpdf] extra. None of these extras is needed for a Python script, so a plain installation is enough for this task.
If you already work in JupyterLab, you do not need to run the command yourself: JupyterLab’s export feature is handled by nbconvert, so having nbconvert available in the same environment is what makes the menu route work. Researchers who manage environments with Anaconda can see Anaconda for Jupyter Notebook and installing Anaconda Python; on a shared server, JupyterHub deployments typically supply a ready environment.
How to convert a Jupyter notebook to a Python script step by step
Method 1: the command line
- Open a terminal in the environment where nbconvert is installed and change to the folder containing the notebook.
- Run
jupyter nbconvert --to script analysis.ipynb, replacinganalysis.ipynbwith your notebook’s name. The same command with--to pythonuses the Python exporter by name. - Look in the notebook’s folder for the new file. By default the output goes to the notebook’s own directory and takes the notebook’s name with the script’s extension, so
analysis.ipynbproduces a.pyfile of the same base name. - Open the file and check the top: for the Python exporter it starts with
#!/usr/bin/env python.
Controlling where the output goes
The configuration reference documents three options that decide where the result lands:
--outputsets the base name of the output file. Its default is{notebook_name}, so the option is only needed to rename the result.--output-dirsets the output directory. The default is empty, which means the notebook’s own directory; a single dot (.) means the current working directory.--stdoutwrites the result to standard output instead of to a file, described in the reference as “Write notebook output to stdout instead of files.” The usage page shows it with a single-file conversion, which suits piping the code into another command.
A combined example is jupyter nbconvert --to script analysis.ipynb --output-dir scripts --output run_analysis. Each flag in it is one of the documented options above. One caution from the usage page applies to conversions that write a notebook: --to notebook mynb --output mynb replaces the input file, and the documentation carries a warning about it. Converting to a script writes a different file type, so it does not overwrite the notebook, but pick an output name deliberately so you do not overwrite an earlier script.
Running the notebook first
Converting to a script copies the code; it does not run it. If you want a notebook executed and its outputs stored, the usage page documents jupyter nbconvert --to notebook --execute mynotebook.ipynb, and adds --allow-errors to continue past cell exceptions instead of stopping at the first failure. That step produces an executed notebook, not a script, so use it as a check that the notebook runs from top to bottom before you convert it, then run the script conversion.
Method 2: the JupyterLab File menu
- Open the notebook in JupyterLab.
- Use the File menu and choose the export option; the JupyterLab export documentation lists “Executable Script (.py)” among the formats offered.
- Save or download the file when prompted.
The JupyterLab documentation notes that the export options depend on how nbconvert is configured and that nbconvert handles the export. The result should therefore match the command-line conversion for the same notebook and configuration. Use the menu when you are already in JupyterLab and want a one-off file; use the command when the conversion is repeated, runs on a server without a browser, or belongs in a documented workflow. For editing and debugging a notebook before conversion, see Jupyter Notebook debugging.
Method 3: Jupytext, keeping notebook and script in sync
Jupytext is a separate package, not part of nbconvert. Its PyPI page gives pip install jupytext and conda install jupytext -c conda-forge as install commands, and documents pairing a notebook with a script using jupytext --set-formats ipynb,py:percent notebook.ipynb, then updating either file from the other with jupytext --sync notebook.py. The py:percent format is the script style that marks cell boundaries. The practical difference from nbconvert is direction: nbconvert is a one-way export, while Jupytext maintains a pair so edits in the script can flow back into the notebook. Choose it when a team wants reviewable text diffs but still works in notebooks.
How to read the output
Open the generated file in a text editor or IDE. Expect the code cells in notebook order, joined into a single file, with the shebang line at the top for the Python exporter. The nbconvert configuration reference lists exporter options that decide what is included: exclude_input, exclude_markdown and exclude_output exist as exporter options, so a team that wants a script without the notebook’s prose can look there rather than deleting lines by hand.
When you read a converted script, check these things yourself, because the conversion does not check them for you:
- Cell order. A notebook can be run out of order, and the script runs in file order. If a result in the notebook depended on a cell run later, the script may fail or give different numbers.
- Environment. Packages imported in the notebook must be installed where the script runs. The shebang line does not choose an interpreter for you if several are installed.
- Paths. Relative data paths resolve from the folder where you launch the script, which may differ from the notebook’s folder.
- Figures. Plots that appeared inline in the notebook are not part of the script. The configuration reference describes an
output_files_dirsetting (default{notebook_name}_files) for files extracted during conversion; a script that must save a figure should do so with an explicit save call in the code.
Treat the first run of a converted script on the same input as a regression test: compare its results with the notebook’s, and record the Python and package versions you used so the analysis can be reproduced. The wider habits behind this are covered in the cluster’s research tools hub.
Troubleshooting nbconvert
The jupyter nbconvert command is not found
nbconvert is installed per environment. Install it with the commands in the install section in the same environment you are running the terminal in, then retry. Activating the wrong conda environment is a common reason the command is missing; this is an editorial observation rather than a documented error message.
The output file is not where you expected
By default the output directory is the notebook’s own directory, not the terminal’s current directory. Use --output-dir . to write to the current directory or give an explicit folder.
You wanted the code on screen, not in a file
Add --stdout. The output goes to the terminal, and you can redirect it with your shell.
Conversion works but the script fails when run
Run the notebook top to bottom first, using jupyter nbconvert --to notebook --execute with --allow-errors only when you deliberately want to see every failing cell in one pass. If the executed notebook fails, the converted script will too. Fix the notebook, then convert again.
A non-HTML format needs extra tools
PDF, LaTeX and other formats depend on Pandoc or TeX as described in the install section. Script conversion does not, so a missing Pandoc error during a script conversion points to the wrong --to value.
Free alternatives to nbconvert
The table lists the routes this guide covers, with what each does according to its own documentation.
| Route | Direction | Where it comes from | Use it when |
|---|---|---|---|
jupyter nbconvert --to script |
Notebook to script, one way | nbconvert, documented usage page | The conversion is repeated or runs on a server |
jupyter nbconvert --to python |
Notebook to Python file, one way | nbconvert PythonExporter | You want the Python exporter named explicitly |
| JupyterLab File menu, “Executable Script (.py)” | Notebook to script, one way | JupyterLab export, handled by nbconvert | You are already in JupyterLab and need one file |
Jupytext pairing with --set-formats and --sync |
Both ways, notebook and script paired | Jupytext package on PyPI | Reviewable text diffs while still working in notebooks |
The data table below summarises the same routes by command so it can be reused. Related pages on this site cover Jupyter Notebook extensions and the IPython kernel that notebooks run on.
Sources
- Project Jupyter nbconvert: Using nbconvert, checked 2026-11-17
- Project Jupyter nbconvert: Installation, checked 2026-11-17
- Project Jupyter nbconvert: Configuration options, checked 2026-11-17
- Project Jupyter nbconvert: Exporters API, checked 2026-11-17
- JupyterLab documentation: Exporting Notebooks, checked 2026-11-17
- Python Package Index: jupytext, checked 2026-11-17
Data: Routes for converting a Jupyter notebook to a Python script
Compares four documented routes for turning an .ipynb notebook into a Python script: the nbconvert script and python exporters, the JupyterLab export menu and Jupytext pairing. Each row gives the command or menu entry, the direction of conversion and the tool that provides it, compiled from the nbconvert, JupyterLab and Jupytext documentation read on 2026-11-17.
| Route | Command or menu entry | Direction | Provided by |
|---|---|---|---|
| nbconvert script exporter | jupyter nbconvert --to script notebook.ipynb | Notebook to script | nbconvert |
| nbconvert Python exporter | jupyter nbconvert --to python notebook.ipynb | Notebook to Python file | nbconvert |
| JupyterLab export | File menu, Executable Script (.py) | Notebook to script | JupyterLab, via nbconvert |
| Jupytext pairing | jupytext --set-formats ipynb,py:percent notebook.ipynb | Notebook and script paired | Jupytext |
Sources
- Using nbconvert (accessed 2026-11-17)
- nbconvert Exporters API (accessed 2026-11-17)
- JupyterLab Exporting Notebooks (accessed 2026-11-17)
- jupytext on PyPI (accessed 2026-11-17)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







