Convert from .py to .ipynb format

Hello
Is there a possibility to convert a file.py 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:

pwd

(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:

ls

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.