269

I've recently moved over to using IPython notebooks as part of my workflow. However, I've not been successful in finding a way to import .py files into the individual cells of an open IPython notebook so that they can edited, run and then saved. Can this be done?

I've found this in the documentation which tells me how to import .py files as new notebooks but this falls short of what I want to achieve.

Any suggestions would be much appreciated.

Davis Broda
  • 4,102
  • 5
  • 23
  • 37
aaronsstack
  • 2,793
  • 3
  • 13
  • 8
  • 6
    Good question. I have yet to see a really satisfying answer. It's especially important when serving an IPython notebook over the internet. If people want to see/edit the source code of functions that are imported (with syntax highlighting, etc) there's currently no easy way to do it. It should be possible to just open py files without transforming them into ipynb files. – Peter Mar 05 '15 at 07:58

6 Answers6

353

EDIT: Starting from IPython 3 (now Jupyter project), the notebook has a text editor that can be used as a more convenient alternative to load/edit/save text files.

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

If you execute a cell containing:

%load 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. Beware that if a file with the same name already exists it will be silently overwritten.

To see the help for any magic command add a ?: like %load? or %%writefile?.

For general help on magic functions type "%magic" For a list of the available magic functions, use %lsmagic. For a description of any of them, type %magic_name?, e.g. '%cd?'.

See also: Magic functions from the official IPython docs.

Brice
  • 352
  • 2
  • 16
user2304916
  • 7,882
  • 5
  • 39
  • 53
  • 3
    This, plus `%save -f` seems to be the way to do what I was looking for. Thanks! – aaronsstack Jan 10 '14 at 02:13
  • 2
    `save -f` is a kind of indirect way of saving into a file and will work only if you execute the cell first and then provide the right reference. In general it's easier to use `%%writefile` that writes the current cell into a file. I updated the answer to mention this. – user2304916 Jul 29 '14 at 03:58
  • 4
    The new magic command is currently `%loadpy` (instead of `%load`). – AlQuemist Jun 10 '16 at 08:26
  • 1
    according to https://ipython.org/ipython-doc/3/interactive/magics.html using `%load` is fine, going forward: "`%loadpy` ( _Alias of_ `%load` ) -- `%loadpy` has gained some flexibility and dropped the requirement of a `.py` extension. So it has been renamed simply into `%load`. You can look at `%load` docstring for more info." – michael May 26 '17 at 23:05
  • Would it be possible to run `%load myfile.py` as a startup parameter for `ipython`? – gies0r Jul 31 '20 at 00:18
146

To write/save

%%writefile myfile.py

  • write/save cell contents into myfile.py (use -a to append). Another alias: %%file myfile.py

To run

%run myfile.py

  • run myfile.py and output results in the current cell

To load/import

%load myfile.py

  • load "import" myfile.py into the current cell

For more magic and help

%lsmagic

  • list all the other cool cell magic commands.

%COMMAND-NAME?

  • for help on how to use a certain command. i.e. %run?

Note

Beside the cell magic commands, IPython notebook (now Jupyter notebook) is so cool that it allows you to use any unix command right from the cell (this is also equivalent to using the %%bash cell magic command).

To run a unix command from the cell, just precede your command with ! mark. for example:

  • !python --version see your python version
  • !python myfile.py run myfile.py and output results in the current cell, just like %run (see the difference between !python and %run in the comments below).

Also, see this nbviewer for further explanation with examples.

starball
  • 20,030
  • 7
  • 43
  • 238
Aziz Alto
  • 19,057
  • 5
  • 77
  • 60
  • 5
    Aziz, this is an excellent answer. One small note: it's very slightly different to use `%run myfile.py` as opposed to `!python myfile.py`. If you do the former it will execute using the ipython kernel, including whatever preloading is configured. A small issue, but I ran into a bug today where it made a difference for me. – Tristan Reid Mar 13 '17 at 21:25
  • If you want to do the equivalent of `%run myfile.py` described in this answer while including IPython cell magic commands in your script, use `%run myfile.ipy`. – Wayne Sep 24 '19 at 00:38
9

Drag and drop a Python file in the Ipython notebooks "home" notebooks table, click upload. This will create a new notebook with only one cell containing your .py file content

Else copy/paste from your favorite editor ;)

Raphaël Braud
  • 1,489
  • 10
  • 14
5

I have found it satisfactory to use ls and cd within ipython notebook to find the file. Then type cat your_file_name into the cell, and you'll get back the contents of the file, which you can then paste into the cell as code.

RussellStewart
  • 5,293
  • 3
  • 26
  • 23
0

I have not found a satisfying answer for this question, i.e how to load edit, run and save. Overwriting either using %%writefile or %save -f doesn't work well if you want to show incremental changes in git. It would look like you delete all the lines in filename.py and add all new lines, even though you just edit 1 line.

0

to write in a file that exists or not use the following

%%writefile script2.py
print(4+5) 
print(5+5)

to append to a file use -a argument

%%writefile -a script2.py
print("hello")

when you load the file

%load script2.py
print(4+5)
print(5+5
print("hello")