Python 如何在 jupyter-notebook 中逐行执行代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38320837/
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 can I execute the code line by line in jupyter-notebook?
提问by user3595632
I'm reading the book, Python Machine Learning
, and tried to analyze the code. But it offers only *.ipynb
file and it makes me very bothersome.
我正在阅读这本书,Python Machine Learning
并试图分析代码。但它只提供*.ipynb
文件,这让我很烦。
For example,
例如,
In this code, I don't want to run whole In[9]
but want to run line by line so that I can check each value of variable and know what each library function do.
在这段代码中,我不想整个运行,In[9]
而是想逐行运行,以便我可以检查变量的每个值并了解每个库函数的作用。
Do I have to comment everytime I want to execute part of codes? I just want something like Execute the block part
like in MATLAB
每次我想执行部分代码时都必须评论吗?我只想要像Execute the block part
这样的东西MATLAB
And also, let say I comment some part of code and execute line by line. How can I check each variable's value without using print()
or display()
? As you know, I don't have to use print()
to check the value in python interactive shell
in terminal. Is there a similar way in Jupyter
?
而且,假设我注释了代码的某些部分并逐行执行。如何在不使用print()
or的情况下检查每个变量的值display()
?如您所知,我不必使用print()
来检查python interactive shell
终端中的值。有没有类似的方法Jupyter
?
回答by Eric Duminil
ast_node_interactivity
ast_node_interactivity
In Jupyter Notebook or in IPython console, you can configure this behaviour with ast_node_interactivity
:
在 Jupyter Notebook 或 IPython 控制台中,您可以使用以下命令配置此行为ast_node_interactivity
:
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
Examples
例子
With this config, every single line will be pretty printed, even if they're in the same cell.
有了这个配置,每一行都会被打印出来,即使它们在同一个单元格中。
- In Notebook:
- 在笔记本中:
- In IPython console:
- 在 IPython 控制台中:
Notes
笔记
None
isn't displayed.There are many other useful tips here("28 Jupyter Notebook tips, tricks and shortcuts - Dataquest").
None
不显示。还有其他许多有用的技巧在这里(“28个Jupyter笔记本技巧,窍门和捷径- Dataquest公司”)。
回答by TheBlackCat
You can just add new cells, then cut-and-paste the parts you want to the new cells. So, for example, you can put the imports and %matplotlib inline
in the first cell (since those only ever need to be run when the notebook is first opened), the y
generation in the second, the X
generation in the third, and the plotting in the fourth. Then you can just run each cell one after another. That is just an example, you can split it up however you want (although I do recommend putting the imports together at the very beginning).
您可以只添加新单元格,然后将您想要的部分剪切并粘贴到新单元格中。因此,例如,您可以将导入 和%matplotlib inline
放在第一个单元格中(因为只有在第一次打开笔记本时才需要运行它们),y
第二个中的X
生成,第三个中的生成,以及第四个中的绘图. 然后你可以一个接一个地运行每个单元格。这只是一个例子,您可以随意拆分它(尽管我建议在一开始就将导入放在一起)。
As for printing, if the last line in a cell is not assigned to a variable, it is automatically printed. So, for example, say the following is a cell:
至于打印,如果单元格中的最后一行未分配给变量,则会自动打印。因此,例如,假设以下是一个单元格:
y = df.iloc[0:100, 4].values
y = np.where(y == 'spam', -1, 1)
y
Then the contents of y
will be displayed after the cell. Similarly, if you have a cell with these contents:
然后y
将在单元格后显示的内容。同样,如果您有一个包含以下内容的单元格:
y = df.iloc[0:100, 4].values
y = np.where(y == 'spam', -1, 1)
y.sum()
Then the result of the y.sum()
operation will be displayed after the cell. On the other hand, if the following cell is executed, then nothing is printed:
然后y.sum()
操作的结果将显示在单元格之后。另一方面,如果执行以下单元格,则不会打印任何内容:
y = df.iloc[0:100, 4].values
y = np.where(y == 'spam', -1, 1)
Nor is anything printed for this one:
也没有为此打印任何内容:
z = {}
y = df.iloc[0:100, 4].values
z['spam'] = np.where(y == 'spam', -1, 1)
回答by Jonathan
In PyCharm Jupyter Notebooks you can just right-click and split cell, the right click merge when you're done.
在 PyCharm Jupyter Notebooks 中,您只需右键单击并拆分单元格,完成后右键单击合并。
回答by user5807327
I think you can print the variables between lines. it is the easy way
我认为您可以在行之间打印变量。这是最简单的方法