如何将文本文件 (.py) 加载/编辑/运行/保存到 IPython 笔记本单元格中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21034373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load/edit/run/save text files (.py) into an IPython notebook cell?
提问by aaronsstack
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?
我最近开始使用 IPython 笔记本作为我工作流程的一部分。但是,我没有成功找到将 .py 文件导入到打开的 IPython 笔记本的单个单元格中的方法,以便它们可以编辑、运行然后保存。这能做到吗?
I've found thisin the documentation which tells me how to import .py files as new notebooks but this falls short of what I want to achieve.
我在文档中找到了这个,它告诉我如何将 .py 文件作为新笔记本导入,但这达不到我想要实现的目标。
Any suggestions would be much appreciated.
任何建议将不胜感激。
采纳答案by user2304916
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.
编辑:从 IPython 3(现在是 Jupyter 项目)开始,笔记本有一个文本编辑器,可以用作加载/编辑/保存文本文件的更方便的替代方法。
A text file can be loaded in a notebook cell with the magic command %load.
可以使用魔法命令在笔记本单元格中加载文本文件%load。
If you execute a cell containing:
如果您执行包含以下内容的单元格:
%load filename.py
the content of filename.pywill be loaded in the next cell. You can edit and execute it as usual.
的内容filename.py将被加载到下一个单元格中。您可以像往常一样编辑和执行它。
To save the cell content back into a file add the cell-magic %%writefile filename.pyat the beginning of the cell and run it. Beware that if a file with the same name already exists it will be silently overwritten.
要将单元格内容保存回文件,请在单元格%%writefile filename.py开头添加 cell-magic并运行它。请注意,如果已存在同名文件,它将被悄悄覆盖。
To see the help for any magic command add a ?: like %load?or %%writefile?.
要查看任何魔术命令的帮助,请添加?:like%load?或%%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?'.
有关魔术函数的一般帮助,请键入“%magic”。有关可用魔术函数的列表,请使用 %lsmagic。对于其中任何一个的描述,键入 %magic_name?,例如 '%cd?'。
See also: Magic functionsfrom the official IPython docs.
另请参阅:官方 IPython 文档中的魔术函数。
回答by Rapha?l Braud
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
在 Ipython notebooks“home” notebooks 表中拖放一个 Python 文件,点击上传。这将创建一个只有一个包含 .py 文件内容的单元格的新笔记本
Else copy/paste from your favorite editor ;)
其他从你最喜欢的编辑器复制/粘贴;)
回答by RussellStewart
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.
我发现在 ipython notebook 中使用 ls 和 cd 来查找文件是令人满意的。然后在单元格中键入 cat your_file_name,您将获得文件的内容,然后您可以将其作为代码粘贴到单元格中。
回答by Aziz Alto
To write/save
写入/保存
%%writefile myfile.py
%%writefile myfile.py
- write/save cell contents into myfile.py (use
-ato append). Another alias:%%file myfile.py
- 将单元格内容写入/保存到 myfile.py(用于
-a追加)。另一个别名:%%file myfile.py
To run
跑步
%run myfile.py
%run myfile.py
- run myfile.py and output results in the current cell
- 运行 myfile.py 并在当前单元格中输出结果
To load/import
加载/导入
%load myfile.py
%load myfile.py
- load "import" myfile.py into the current cell
- 将“导入”myfile.py 加载到当前单元格中
For more magic and help
获取更多魔法和帮助
%lsmagic
%lsmagic
- list all the other cool cell magic commands.
- 列出所有其他很酷的单元格魔法命令。
%COMMAND-NAME?
%COMMAND-NAME?
- for help on how to use a certain command. i.e.
%run?
- 有关如何使用某个命令的帮助。IE
%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 %%bashcell magic command).
除了 cell 魔法命令,IPython notebook(现在是 Jupyter notebook)非常酷,它允许你直接从 cell 使用任何 unix 命令(这也相当于使用%%bashcell magic 命令)。
To run a unix command from the cell, just precede your command with !mark. for example:
要从单元中运行 unix 命令,只需在命令前加上!标记即可。例如:
!python --versionsee your python version!python myfile.pyrun myfile.py and output results in the current cell, just like%run(see the difference between!pythonand%runin the comments below).
!python --version查看你的python版本!python myfile.py运行 myfile.py 并在当前单元格中输出结果,就像%run(参见下面评论中的!python和之间的区别%run)。
Also, see this nbviewerfor further explanation with examples. Hope this helps.
另外,请参阅此 nbviewer以获取示例的进一步说明。希望这可以帮助。

