如何将文本文件(.py)加载/编辑/运行/保存到IPython Notebook单元中?

问题:如何将文本文件(.py)加载/编辑/运行/保存到IPython Notebook单元中?

我最近已将使用IPython笔记本作为工作流程的一部分。但是,我没有成功找到一种方法来将.py文件导入打开的IPython Notebook的各个单元中,以便可以对其进行编辑,运行和保存。能做到吗?

我在文档中找到了这一点,该文档告诉我如何将.py文件作为新笔记本导入,但是这与我想要实现的目标不符。

任何建议将不胜感激。

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.


回答 0

编辑:从IPython 3(现在为Jupyter项目)开始,笔记本具有文本编辑器,可以用作加载/编辑/保存文本文件的更方便的替代方法。

可以使用magic命令将文本文件加载到笔记本单元中%load

如果执行包含以下内容的单元格:

%load filename.py

的内容filename.py将在下一个单元格中加载。您可以照常编辑和执行它。

要将单元格内容保存回文件中,请在单元格%%writefile filename.py的开头添加cell-magic 并运行它。请注意,如果已经存在同名文件,它将被静默覆盖

要查看任何魔术命令的帮助,请添加?:like %load?%%writefile?

有关魔术功能的常规帮助,请键入“%magic”。有关可用魔术功能的列表,请使用%lsmagic。对于其中任何一个的描述,请键入%magic_name ?,例如’%cd?’。

另请参见:官方IPython文档中的Magic函数

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.


回答 1

写入/保存

%%writefile myfile.py

  • 将单元格内容写入/保存到myfile.py中(用于-a追加)。另一个别名:%%file myfile.py

跑步

%run myfile.py

  • 运行myfile.py并在当前单元格中输出结果

加载/导入

%load myfile.py

  • 将“导入” myfile.py加载到当前单元格中

寻求更多的魔术和帮助

%lsmagic

  • 列出所有其他酷单元魔术命令。

%COMMAND-NAME?

  • 获取有关如何使用特定命令的帮助。即%run?

注意

除了单元魔术命令之外,IPython Notebook(现在为Jupyter笔记本)非常酷,它允许您直接使用单元中的任何Unix命令(这也等同于使用%%bashcell magic命令)。

要从单元格运行unix命令,只需在命令前加上!标记。例如:

  • !python --version 查看您的python版本
  • !python myfile.py运行myfile.py并在当前单元格中输出结果,就像%run(请参阅!python%run下面的注释之间的区别)一样。

另外,请参阅此nbviewer以获取有关示例的进一步说明。希望这可以帮助。

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. Hope this helps.


回答 2

将一个Python文件拖放到Ipython笔记本的“ home”笔记本表中,单击“上传”。这将创建一个只有一个包含.py文件内容的单元格的新笔记本

从您喜欢的编辑器中进行其他复制/粘贴;)

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 ;)


回答 3

我发现在ipython笔记本中使用ls和cd查找文件是令人满意的。然后,在单元格中键入cat your_file_name,您将获得文件的内容,然后可以将其作为代码粘贴到单元格中。

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.