Skip to content
FlipConverto home

IPYNB to PY Converter

Convert ipynb to py: open a Jupyter notebook and get a Python script, with code cells as code, markdown cells as # comments and IPython magics commented out so the file runs as plain Python.

Loading IPYNB to PY Converter…

Runs entirely in your browser. Files stay on your device — nothing is uploaded. Verify it yourself

How do you convert a Jupyter notebook to a Python script?

Take the source of each code cell in order, turn markdown cells into # comments, and comment out IPython-only lines such as %matplotlib and !pip so the file is valid Python. This tool does that from the .ipynb file and leaves outputs out.

Cost
Free, no limits
Upload
None — runs on your device
Sign-up
Not required
Works offline
Yes, once loaded

How to convert an ipynb notebook to a .py file

  1. Open the notebook

    Press Open .ipynb file and choose it, or paste the notebook’s JSON into the box.

  2. Pick the options

    Keep or drop markdown cells, and choose whether to mark cells with # %%.

  3. Check what changed

    The counts show code cells, markdown cells and commented magics; any warning, such as a non-Python kernel, appears above the script.

  4. Download the .py file

    Download .py saves the script under the notebook’s name, or copy it.

Examples

  • Markdown cell: # Results Code cell: %matplotlib inline import pandas as pd# %% [markdown] # # Results # %% # %matplotlib inline import pandas as pd

    With cell markers on.

  • %%bash echo start# %%bash # echo start

    A cell in another language is commented out whole.

Key features of the IPYNB to PY Converter

  • Reads nbformat 4 notebooks, and nbformat 3 files with worksheets and heading cells
  • Copies code cells in order and turns markdown and raw cells into # comments, or drops them
  • Optional # %% cell markers that VS Code, Spyder and PyCharm run as cells
  • Comments out % and %% magics, ! shell commands, captures and ? help lookups
  • Leaves saved outputs out, so the script is just the code
  • Opens the file in your browser; the notebook and its data stay on your device

What it converts

Input

  • Jupyter .ipynb (nbformat 4)
  • Older nbformat 3 notebooks

Output

  • .py script
  • Optional # %% cell markers (percent format)

Commented out

  • % line magics
  • %% cell magics
  • ! shell commands
  • ? help lookups

When should you use this?

  • Turning an exploratory notebook into a script for a scheduled job or a pipeline.
  • Reviewing notebook code in a pull request as a readable .py diff.
  • Opening a notebook’s code in an editor that does not read .ipynb files.

Limitations

  • Outputs, images and widget state saved in the notebook are not included.
  • Magics are commented out, not translated: code that relied on %run, %load_ext or ! output needs replacing with Python equivalents.
  • A magic indented inside an if or for block is commented too, which can leave that block empty; add pass or a Python equivalent.
  • Notebook files are limited to 5 MB — large saved outputs are the usual reason a notebook is bigger; clear them in Jupyter first.

Troubleshooting

"not valid notebook JSON"

The file was saved as something else — an HTML export or a script — or was cut short. Download the .ipynb again from Jupyter with File → Download.

The script fails where a magic used to be

Replace the commented line with Python: for example subprocess.run for a ! shell command, or import the module a %load_ext loaded.

Common mistakes to avoid

  • Assuming variables from a !command capture still exist. The commented line no longer runs, so later code that uses them fails.
  • Running the script from a different folder than the notebook. Relative file paths resolve from where you run Python.

Pro tips

Tips for better results

  • File size

    Clear outputs first to shrink big notebooks

    Clear all outputs in Jupyter before converting big notebooks; it shrinks the file without changing the code.

  • Editors

    Keep # %% markers for notebook-style cells in VS Code

    Keep the # %% markers if you will keep working in VS Code — you get notebook-style cells in a plain .py file.

  • Packages

    Put dependencies in requirements.txt, not !pip lines

    Install packages from a requirements.txt rather than !pip lines, so the script and the notebook share one list.

What goes into the script

A .ipynb file is JSON: a list of cells plus their saved outputs. Code cells are copied as they are, separated by two blank lines. Markdown cells become comment lines starting with #, or are dropped if you only want the code. Raw cells are treated the same way as markdown. Outputs — printed results, tables, charts — and execution counts are not included, because a script recreates them by running.

With “Mark cells with # %%” on, every cell starts with # %% (and markdown cells with # %% [markdown]). VS Code, Spyder and PyCharm show each marked block as a cell you can run on its own, and Jupytext can turn the file back into a notebook.

Magics and shell commands

Notebooks run on IPython, which accepts syntax Python does not: line magics such as %matplotlib inline, shell commands such as !pip install pandas, captures such as files = !ls, and help lookups such as len?. Left in, any of these is a syntax error in a .py file, so they are commented out and counted.

Cell magics are handled by what the cell contains. In %%time, %%timeit, %%capture, %%prun, %%debug, %%memit and %%skip cells the body is Python, so only the %% line is commented. Cells such as %%bash, %%html, %%sql or %%writefile hold another language or file content, so the whole cell is commented.

Compared with nbconvert

Running jupyter nbconvert --to script notebook.ipynb does a similar job on your own computer, but it rewrites magics as get_ipython() calls, so the result only runs under IPython. This tool comments them out instead, so the script runs with plain python — and you can see at a glance what used to be a magic.

Sources

Privacy and file handling

The IPYNB to PY Converter runs entirely in your browser. Your file is read by the page, processed on your own device, and the result is handed straight back to you — no copy is sent anywhere, because none needs to be.

You can confirm this rather than take our word for it: open your browser’s developer tools, watch the Network tab, and use the tool. No request carries your file or your input. It also keeps working with the connection off.

Details are in the privacy policy.

What next?

See what changed between two scripts line by line.

Compare two versions

IPYNB to PY Converter — questions

How do I convert an .ipynb file to a .py file?

Press Open .ipynb file and choose the notebook, or paste its JSON. The Python script appears below; Download .py saves it with the notebook’s name.

What happens to %matplotlib inline and !pip install?

They are commented out, because they are IPython syntax and would be errors in a plain Python file. The count of commented lines is shown above the script.

Are cell outputs included?

No. Printed output, tables and charts saved in the notebook are left out; running the script produces them again.

What does # %% do?

It marks the start of a cell. VS Code, Spyder and PyCharm let you run each # %% block on its own, much like a notebook cell, and Jupytext reads the same markers.

Can it convert notebooks in R or Julia?

It copies the code, but the file is named .py and the code is not Python, so a notice warns you when the notebook’s kernel language is not Python.

Part of Developer Tools · Browse every tool