7

On my old computer, I was able to run .py files from Jupyter Notebook, edit them, and run them. The .py file was effectively a notebook file for all intents and purposes. I updated to the latest version of notebook, and I am no longer able to do this. How do I use .py files on my notebook?

I know there are roundabout ways to do this. I am looking for the method where, when you are in notebook, instead of opening a .ipynb file, you select a .py file which is opened, and behaves like a .ipnyb. When you save it, it writes to .py.

David Kong
  • 578
  • 1
  • 5
  • 20
  • 1
    Possible duplicate of [How to load/edit/run/save text files (.py) into an IPython notebook cell?](https://stackoverflow.com/questions/21034373/how-to-load-edit-run-save-text-files-py-into-an-ipython-notebook-cell) – Fullstack Guy Sep 15 '19 at 18:51
  • It isn't, I know there are roundabout ways to do this. I was able to get it working directly on my other computer, which I unfortunately no longer have access to. – David Kong Sep 15 '19 at 19:24

5 Answers5

4

A text file can be loaded in a notebook cell with the magic command %load.

If you execute a cell containing:

%loadpy filename.py


The content of filename.py will be loaded in the next cell. You can edit and execute it as usual.

To save the cell content back into a file add the cell-magic
%%writefile filename.py
at the beginning of the cell and run it.

To see the help for any magic command add a ?: like %loadpy? or %%writefile?.
%COMMAND-NAME?
i.e. %run?

For list of available magic function use %lsmagic. Alternatively there is also another method magic function called %save-f but I would not recommend that, it's an indirect way of saving files.

Also see -
1. Magic Functions docs
2.this nbviewer for further explanation with examples.

Hope this helps.

Rishit Dagli
  • 1,000
  • 8
  • 20
  • 1
    @David Kong , this is working for me, also tried the link on mobile, it's working. I would suggest you to close the website and reload again, maybe it will work. I am also pasting the link here try this too. https://nbviewer.jupyter.org/github/ipython/ipython/blob/1.x/examples/notebooks/Cell%20Magics.ipynb – Rishit Dagli Oct 16 '19 at 10:16
4

This is not the exact answer. At one point, I was able to open .py files using python notebook and work on it as if it were a notebook file.

However, I have been able to replicate this behavior using VScode.

https://code.visualstudio.com/docs/python/jupyter-support-py

Using VScode, you can export all your .ipynb files into .py files, then run code blocks. Code blocks are separated by # %%.

I have not used it sufficiently long enough to decide if it is better than python notebook, but this seems to be the best solution so far. I previously tried using Atom/Hydrogen and did not enjoy the experience.

David Kong
  • 578
  • 1
  • 5
  • 20
1

You can save individual cells as files using the following code: %%writefile some_file_name.py.

You can run that code straight from the terming or from another notebook using the following code: %run some_file_name.py

Yaakov Bressler
  • 9,056
  • 2
  • 45
  • 69
0

Some editors (like spyder and vscode) have jupyter notebook functionality. These can be used if jupyter in installed in the python environment.

You can use it by add #%% on top of the block of code. (in vscode the button 'run cell' will automatically appear)

Also it is possible to import .ipynb as .py which can be run in to fancy decrypt above.

Tom Nijhof
  • 542
  • 4
  • 11
0

I just found this package p2j and tested it with a .py file with functions, comments and normal code.

I used it as indicated in this answer by doing the following:

pip install p2j
p2j -o script.py -t new_file.ipynb

You can also add -o flag to overwrite the original file.

With this, I got a working Jupiter Notebook with each block of code in a cell and the comments as markdown.

Example:

Original .py script

original .pz

Converted .ipynb

converted .ipynb

daco
  • 600
  • 1
  • 4
  • 14