61

I wrote a juypter notebook that has been converted to .py somehow. I would like it back in the original format. Does anyone know how to do that?

There is a previous stack overflow question about this, but the solution doesn't work for me. Converting to (not from) ipython Notebook format

1
  • 9
    This question seems misleading. As far as I can tell this file has not been "converted to .py", it is just a notebook file incorrectly saved with the .py extension. Just change the extension back to .ipynb. Commented Dec 7, 2021 at 0:09

8 Answers 8

62

Use p2j to convert Python source code to Jupyter Notebook.

From the command line, run

-->pip install p2j 

then go to the directory where your file is located. -->( for example-> cd downloads, if the file is in download directory)

then run

-->p2j myscript.py 

This will create a myscript.ipynb file.

Sign up to request clarification or add additional context in comments.

2 Comments

This is not at all efficieent. It has commented all the lines just after the first comment.
Works pretty well : it converted all "main comments" (i.e. starting with '#' and no leading space) to MarkDown cells, and assigned each function definition to a dedicated cell.
27

You really should consider using jupytext

Run conda install jupytext or pip install jupytext

Then do: jupytext --set-formats ipynb,py <file>.ipynb

This will create the .ipynb file and for an additional bonus keep it synchronized to the .py file:

jupytext --set-formats ipynb,py <file>.ipynb --sync

This will make sure jupyter keeps the two files in sync when saving from now on...

Last note: If you are a gui person, after running the installation command for jupytext, everything else can be done from the gui as well File-->jupytext-->pair Notebook with light Script: enter image description here

4 Comments

I really like what's described from here :) If only it can annotate the file :D Because I'm a Software Engineer building Data Sciences LOL I modularized and containerize everything :D I want to keep both Software and Data Engineers working in harmony :) each on their own world
Depends what you mean by annotation... I think every cell will be annotated so that it can be reconstructed... E.g. for cell [3], it will be preceded by #[3] etc.
Is there a way to use this with Pycharm?
If you run jupytext --set-formats ipynb,py *.ipynb in a folder, Will create .ipynb files for each .py files in that folder.
13

The question was edited so this isn't as direct of an answer as it once was. Nevertheless, if you accidentally changed the extension of your Python notebook from .ipynb to .py as the OP did when originally asking the question, this is the answer for you.

Just rename it changing the extension e.g. for linux/macos

mv <file>.py <file>.ipynb 

or right-click rename for windows and type the full name with the extension

(Since it seems that the contents are .ipynb contents already)

3 Comments

Thank you so much! This works perfectly. I will accept the answer in 8 minutes when I can - thanks for answering the question so quickly.
This method does not work for me. The error message when trying to open the jupyter file is as follows: Unreadable Notebook: C:\Users\Wei-shan\Desktop\python notebooks\regression-trees\cart_weishan.ipynb NotJSONError('Notebook does not appear to be JSON: \'"""Implementation of the CART algorithm...')
The method in the answer only works if you have a notebook that is saved as .py (which is what the question was about), not as a general conversion mechanism from .py to .ipynb
9

ipynb-py-convert

By following below steps you can get .ipynb file

Install "pip install ipynb-py-convert" Go to the directory where the py file is saved via command prompt Enter the command

ipynb-py-convert YourFileName.py YourFilename.ipynb

Eg:. ipynb-py-convert getting-started-with-kaggle-titanic-problem.py getting-started-with-kaggle-titanic-problem.ipynb

Above command will create a python script with the name "YourFileName.ipynb" and as per our example it will create getting-started-with-kaggle-titanic-problem.ipynb file

1 Comment

Works good, converted .py file without problem
6
jupytext --to notebook <name_of_script_file>.py 

Comments

4

Renaming won't work if the py fils were py from the beginning. The easiest way might be to run the following script from PowerShell:

  1. Install ipynb-py-convert:

    pip install ipynb-py-convert

  2. jump to your directory level

    cd "YOUR_DIRECTORY_PATH"

  3. Convert all files recursively (also in all subdirectories):

    foreach ($f in Get-ChildItem "." -Filter *.py -Recurse){ ipynb-py-convert $f.FullName "$($f.FullName.Substring(0,$f.FullName.Length-3)).ipynb"}

Cheers!

Comments

4

Install jupytext package (# pip install jupytext)

import jupytext notebook = jupytext.read('example.py') jupytext.write(notebook, 'example.ipynb', fmt='.ipynb') 

Comments

1

If you wish to apply the conversion of all the .py files rooted in your project directory , you can run the following command in batch file:

for /r %%v in (*.py) do p2j "%%v" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.