简体   繁体   English

从 Jupyter Notebook 运行并保存到 .py 文件

[英]Run from and save to .py file from Jupyter Notebook

On my old computer, I was able to run .py files from Jupyter Notebook, edit them, and run them.在我的旧电脑上,我能够从 Jupyter Notebook 运行 .py 文件,编辑并运行它们。 The .py file was effectively a notebook file for all intents and purposes. .py 文件实际上是一个用于所有意图和目的的笔记本文件。 I updated to the latest version of notebook, and I am no longer able to do this.我更新到了最新版本的 notebook,我再也无法这样做了。 How do I use .py files on my notebook?如何在笔记本上使用 .py 文件?

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.我正在寻找一种方法,当您在笔记本中时,不是打开 .ipynb 文件,而是选择一个打开的 .py 文件,其行为类似于 .ipnyb。 When you save it, it writes to .py.当您保存它时,它会写入 .py。

A text file can be loaded in a notebook cell with the magic command %load.可以使用魔术命令 %load 在笔记本单元格中加载文本文件。

If you execute a cell containing:如果您执行包含以下内容的单元格:

%loadpy filename.py


The content of filename.py will be loaded in the next cell. filename.py的内容将在下一个单元格中加载。 You can edit and execute it as usual.您可以像往常一样编辑和执行它。

To save the cell content back into a file add the cell-magic要将单元格内容保存回文件,请添加 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? : 像%loadpy? or %%writefile?%%writefile? . .
%COMMAND-NAME?
ie %run?%run?

For list of available magic function use %lsmagic .有关可用魔法函数的列表,请使用%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.或者,还有另一种称为%save-f方法魔术函数,但我不建议这样做,它是一种间接保存文件的方法。

Also see -另见——
1. Magic Functions docs 1. 魔法函数文档
2.this nbviewer for further explanation with examples. 2.this nbviewer用例子进一步解释。

Hope this helps.希望这可以帮助。

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.有一次,我能够使用 python notebook 打开 .py 文件,并像处理 notebook 文件一样处理它。

However, I have been able to replicate this behavior using VScode.但是,我已经能够使用 VScode 复制这种行为。

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

Using VScode, you can export all your .ipynb files into .py files, then run code blocks.使用 VScode,您可以将所有 .ipynb 文件导出为 .py 文件,然后运行代码块。 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.我还没有使用它足够长的时间来决定它是否比 python notebook 更好,但这似乎是迄今为止最好的解决方案。 I previously tried using Atom/Hydrogen and did not enjoy the experience.我之前尝试过使用 Atom/Hydrogen,但并不喜欢这种体验。

You can save individual cells as files using the following code: %%writefile some_file_name.py .您可以使用以下代码将单个单元格保存为文件: %%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您可以使用以下代码直接从术语或另一个笔记本中%run some_file_name.py该代码: %run some_file_name.py

Some editors (like spyder and vscode) have jupyter notebook functionality.一些编辑器(如 spyder 和 vscode)具有 jupyter notebook 功能。 These can be used if jupyter in installed in the python environment.如果 jupyter 安装在 python 环境中,则可以使用这些。

You can use it by add #%% on top of the block of code.您可以通过在代码块顶部添加 #%% 来使用它。 (in vscode the button 'run cell' will automatically appear) (在 vscode 中,“运行单元格”按钮会自动出现)

Also it is possible to import .ipynb as .py which can be run in to fancy decrypt above.也可以将 .ipynb 导入为 .py,它可以运行在上面的花式解密中。

I just found this package p2j and tested it with a .py file with functions, comments and normal code.我刚刚找到了这个包p2j并用一个带有函数、注释和普通代码的 .py 文件对其进行了测试。

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.您还可以添加 -o 标志来覆盖原始文件。

With this, I got a working Jupiter Notebook with each block of code in a cell and the comments as markdown.有了这个,我得到了一个工作的 Jupiter Notebook,每个代码块都在一个单元格中,注释作为降价。

Example:例子:

Original .py script原始 .py 脚本

原始.pz

Converted .ipynb转换后的 .ipynb

转换后的 .ipynb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM