Convert from .py to .ipynb format

Is there a possibility to convert a into a file.ipynb in Jupyterlab?
I’ve tried to open the .py file and then save as .ipynb file but I get an error message.
There should be an efficient way to transform a file from one format to the other one.
Thank you

In a notebook, do the following in a cell:

%pip install jupytext
!jupytext --to notebook <name_of_script_file>.py

Replacing <name_of_script_file> with name of your Python script. You may need to specify the path if your working directory in the notebook and where the script is aren’t the same.

Or you could use the terminal to run jupytext if you comfortable. The magic install though insures it will work in whatever environment that notebook happened to be running in.

See here for more about Jupytext use.

There’s also the use of the %load command that may do what you require?
See here and here.

1 Like

Thank you very much for your answer!
I don’t know what I do wrong but it doesn’t work.

thank you

You most likely aren’t working in the correct directory in your notebook.

Note that I said this in my post:

You are using JupyterLab it appears. The file navigation panel on the left and the running notebook are not linked; just because you are looking at the target notebook you wish to convert in the panel on the left, doesn’t mean that is in the current working directory in your active notebook on the right. To check the current working directory in your active notebook, type the following in a cell in it and run it:


(By the way, that means ‘print working directory’. See my links and notes here if these concepts aren’t familiar to you.)

I suspect you’ll see it doesn’t match your location for the notebook you are trying to point Jupytext at for conversion. The way you have written your !jupytext command, it isn’t going to work until you can enter the following in the notebook cell and see the notebook you are hoping to convert among the list of files in the current working directory:


So one solution is to specify the absolute path to that notebook in your !jupytext command. Right now you are relying on a relative path, in this case where the notebook file and the running notebook are expected to be in the same directory relative to each other.

Another possible solution is to close the notebook you currently show because jupytext is installed. And just start a new one from the launcher after you have navigated in the file navigation panel to the directory containing the target notebook you wish to convert. You should then see the new notebook get created in the same directory as the notebook you are targeting for conversion and now your !jupytext command.

I’ll add for the sake of improved completeness, that within the active notebook, it is also possible to change the working directory, see about the line magic command %cd here. You’d need to navigate relative or use an absolute path, as well.

Another possibility is to navigate to the directory with the notebook in the terminal launched from JupyterLab, and then run your jupytext command, but now without the exclamation mark as you are directly on the command line already.

Thank you for your reply.
I still don’t get it. I’ve converted the file “” into “Beta.ipynb”.

But when I open it I get only HTML code.

this is what I see at the top of the file when I open it:

It would have liked to see what had happened if you ran !head DIGA/Beta.ipynb after the first time you ran it in cell #1 of what you posted. You’ll note there is a difference in cell #2 in the information Jupytext gives after the same command.

I think you are seeing stuff from OneDrive being in play here. Take that out of the equation unless you follow how to get the actual file.

I can give you a way to do that. Go here and click launch binder. I have jupytext already installed there. After the session spins up, click on Jupyter logo in the upper left to go to the dashboard, and then upload your .py file to the secure, temporary remote session. The Upload button is on the right in the dashboard. Once it is uploaded, run your conversion command in a new notebook in that session, pointing it at your script file. Then open the notebook produced there. You should see a notebook.