Why convert PY to IPYNB?
Researchers and data scientists use .ipynb notebooks in JupyterLab to run code interactively, see inline plots, and add markdown explanations between code sections. Converting a plain .py script to .ipynb makes it runnable step-by-step in that environment without rewriting the code.
How to convert PY to IPYNB
jupytext OPEN-SOURCE
Run pip install jupytext then jupytext --to notebook file.py; place # %% comments in the .py source to define cell boundaries before converting for the best result.
Jupyter (nbconvert) FREE
Run jupyter nbconvert --to notebook --execute file.py to convert and run the script storing outputs in the .ipynb; omit --execute to convert without running.
About these formats
A .py file is a Python source code file - plain text containing a program or script written in the Python language. To read the code, open it in any text editor. To run it, install Python…
Open .PY details →A .IPYNB file is associated with IPython notebook, a misc file.
Open .IPYNB details →Quality & what to watch
- Cell boundaries are guessed when no
# %%markers are present - add# %%to your.pyfile before converting to control exactly where each cell starts. - The converted
.ipynbcontains no cell outputs; plots, print statements, and other results are generated when you run the notebook in JupyterLab. - nbconvert treats the entire
.pyas a single code cell unless the file already contains# In[]:cell markers; jupytext handles plain scripts more gracefully.
Frequently asked questions
Will my Python imports and functions work the same in the notebook?
Can I convert the notebook back to a .py file?
jupytext --to py file.ipynb to go back to a plain Python script.What is the difference between jupytext and nbconvert for this task?
# %% markers; nbconvert is built into Jupyter and can execute the code and store outputs during conversion.