Convert ipynb to py
Turn a Jupyter notebook into a Python script in your browser. Free, and the file never leaves your computer.
Your notebook starts here
Open a local file to preview its contents. No account needed.
or drop the notebook anywhere on this page
See the result first.
A script that actually runs
An .ipynb is JSON holding code cells, Markdown cells and saved outputs. A script needs only the code, in order, with the notebook-only lines dealt with. This converter writes the code cells out, keeps the Markdown as comments, and comments out %magics and !shell lines — the two things that make an exported notebook crash under plain python.
How to convert a notebook to a Python script
- 1
Pick your file
Choose a notebook, or drop it on the page. Python, R and Julia kernels are recognised and the script gets the matching extension.
- 2
Check the script
The whole file is shown, highlighted. Choose how cells are marked, whether Markdown stays as comments, and what happens to %magics.
- 3
Download .py
The script is written straight to disk. Or copy it and paste it into an editor.
Five ways to get a .py out of a notebook
They all write the same code cells. They differ on the three lines that are not Python — magics, Markdown and cell markers — and that is what decides whether the file runs.
| Method | Needs installing | %magics and !shell | Markdown | Cell markers |
|---|---|---|---|---|
| This converter | Nothing | Commented out, script runs | Comments, or dropped | # %%, # In[n]:, or none |
| nbconvert --to script | Python + nbconvert | Rewritten to get_ipython() calls — fails outside IPython | Comments | # In[n]: |
| JupyterLab export | Jupyter | Same as nbconvert | Comments | # In[n]: |
| VS Code export | Jupyter extension | Kept as written | Comments | # %% |
| Jupytext | Python + jupytext | Commented, reversible | Comments | # %%, several formats |
The command-line route, for when you have Python anyway:
jupyter nbconvert --to script notebook.ipynb
Then open the result and look for get_ipython(). Every one of those lines was a magic, and every one will raise NameError until you delete or comment it. That is the step this page does for you.
What the script is for
Put it under version control
A .py diffs line by line. An .ipynb is JSON with outputs and base64 images inside, and every run changes it. Commit the script next to the notebook, or instead of it.
Turn it into a module
Once the code is a plain file you can import it, add a main guard, and run it from a scheduler or a pipeline without a kernel.
Hand in a .py
Some courses and code reviews want a script, not a notebook. Keep the headings as comments and the structure of the write-up survives.
Want the outputs kept? Convert to PDF · Convert to HTML · View the notebook online
Have a script and need a notebook? Convert py to ipynb
How it works, and why you can trust it
The converter is JavaScript running in this page. It reads the notebook JSON, takes the source of each cell in order, and writes the script; the preview is highlighted with highlight.js. There is no upload endpoint, so the file cannot leave your machine.
The source is on GitHub under the MIT licence.
Questions
- Is my notebook uploaded?
- No. The file is read and converted by JavaScript on your own computer. There is no server to send it to.
- Why does the script from nbconvert fail with NameError: get_ipython?
- nbconvert turns every IPython magic — %matplotlib inline, %%time, !pip install — into a call like get_ipython().run_line_magic(...). That function only exists inside IPython, so running the file with plain python raises NameError. This converter comments those lines out instead, so the script runs and you can still see what was there.
- What happens to the outputs?
- They are not part of a script. A .py holds the code that produced them; run it and the outputs come back. If you want a file that keeps the plots and tables, convert to HTML or PDF instead.
- What do the cell markers do?
- # %% is a comment to Python, but VS Code, Spyder and PyCharm read it as a cell boundary and let you run one cell at a time, like a notebook. # In[3]: is the marker nbconvert writes and carries the execution count. None gives you a plain script with a blank line between cells.
- Where do the Markdown cells go?
- Each line becomes a comment, so headings and explanations stay in the file where they were. Choose Drop for code only.
- Does it work with R and Julia notebooks?
- Yes. The kernel language is read from the notebook's metadata: an R kernel gives you an .R file, Julia a .jl. All three comment with #, so Markdown cells work the same way. Magic handling is only applied to Python.
- Can I convert a .py back to .ipynb?
- Yes, with the py to ipynb converter on this site. It reads the # %% markers this page writes, so a script from here goes back into a notebook with the same cells and the magics restored.
- Is it free?
- Yes. No account, no watermark, no file limit. The code is open source under the MIT licence.