在 iPython notebook 中调试的正确方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32409629/
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
What is the right way to debug in iPython notebook?
提问by Rex
As I know, %debug magic
can do debug within one cell.
据我所知,%debug magic
可以在一个单元格内进行调试。
However, I have function calls across multiple cells.
但是,我有跨多个单元格的函数调用。
For example,
例如,
In[1]: def fun1(a)
def fun2(b)
# I want to set a breakpoint for the following line #
return do_some_thing_about(b)
return fun2(a)
In[2]: import multiprocessing as mp
pool=mp.Pool(processes=2)
results=pool.map(fun1, 1.0)
pool.close()
pool.join
What I tried:
我试过的:
I tried to set
%debug
in the first line of cell-1. But it enter into debug mode immediately, even before executing cell-2.I tried to add
%debug
in the line right before the codereturn do_some_thing_about(b)
. But then the code runs forever, never stops.
我试图
%debug
在cell-1的第一行设置。但它会立即进入调试模式,甚至在执行 cell-2 之前。我试图
%debug
在代码之前的行中添加return do_some_thing_about(b)
。但是代码永远运行,永不停止。
What is the right way to set a break point within the ipython notebook?
在 ipython 笔记本中设置断点的正确方法是什么?
采纳答案by Tevin Joseph K O
Use ipdb
使用ipdb
Install it via
通过安装它
pip install ipdb
Usage:
用法:
In[1]: def fun1(a):
def fun2(a):
import ipdb; ipdb.set_trace() # debugging starts here
return do_some_thing_about(b)
return fun2(a)
In[2]: fun1(1)
For executing line by line use nand for step into a function use sand to exit from debugging prompt use c.
用于逐行执行n和单步执行函数使用s以及退出调试提示使用c。
For complete list of available commands: https://appletree.or.kr/quick_reference_cards/Python/Python%20Debugger%20Cheatsheet.pdf
有关可用命令的完整列表:https: //appletree.or.kr/quick_reference_cards/Python/Python%20Debugger%20Cheatsheet.pdf
回答by Mohit Khurana
Your return function is in line of def function(main function), you must give one tab to it. And Use
你的返回函数在 def 函数(主函数)的一行,你必须给它一个标签。并使用
%%debug
instead of
代替
%debug
to debug the whole cell not only line. Hope, maybe this will help you.
调试整个单元格不仅行。希望,也许这会帮助你。
回答by krock
You can always add this in any cell:
您始终可以在任何单元格中添加它:
import pdb; pdb.set_trace()
and the debugger will stop on that line. For example:
调试器将在该行停止。例如:
In[1]: def fun1(a):
def fun2(a):
import pdb; pdb.set_trace() # debugging starts here
return fun2(a)
In[2]: fun1(1)
回答by Leopd
You can use ipdb
inside jupyter with:
您可以ipdb
在 jupyter 中使用:
from IPython.core.debugger import Tracer; Tracer()()
Edit: the functions above are deprecated since IPython 5.1. This is the new approach:
编辑:自 IPython 5.1 起不推荐使用上述函数。这是新方法:
from IPython.core.debugger import set_trace
Add set_trace()
where you need a breakpoint. Type help
for ipdb
commands when the input field appears.
set_trace()
在需要断点的地方添加。键入help
用于ipdb
命令输入字段出现时。
回答by flowera
Just type import pdb
in jupyter notebook, and then use this cheatsheetto debug. It's very convenient.
只需键入import pdb
在jupyter笔记本,然后用这个的cheatsheet调试。这很方便。
c
--> continue, s
--> step, b 12
--> set break point at line 12 and so on.
c
--> continue, s
--> step, b 12
--> 在第 12 行设置断点等等。
Some useful links: Python Official Document on pdb, Python pdb debugger examples for better understanding how to use the debugger commands.
一些有用的链接: 关于 pdb 的 Python 官方文档,用于更好地理解如何使用调试器命令的 Python pdb 调试器示例。
回答by waterproof
The %pdb
magic commandis good to use as well. Just say %pdb on
and subsequently the pdb
debugger will run on all exceptions, no matter how deep in the call stack. Very handy.
在%pdb
魔术的命令是很好用为好。只是说%pdb on
,随后pdb
调试器将在所有异常上运行,无论调用堆栈有多深。非常便利。
If you have a particular line that you want to debug, just raise an exception there (often you already are!) or use the %debug
magic command that other folks have been suggesting.
如果您有要调试的特定行,只需在那里引发异常(通常您已经是!)或使用%debug
其他人一直建议的魔术命令。
回答by Vlad Bezden
In Python 3.7 you can use breakpoint()function. Just enter
在 Python 3.7 中,您可以使用breakpoint()函数。只需输入
breakpoint()
wherever you would like runtime to stop and from there you can use the same pdb commands (r, c, n, ...) or evaluate your variables.
无论您希望运行时停止并从那里停止,您都可以使用相同的 pdb 命令(r、c、n、...)或评估您的变量。
回答by Escachator
After you get an error, in the next cell just run %debug
and that's it.
出现错误后,在下一个单元格中运行即可%debug
。
回答by Alessandro Dentella
I just discovered PixieDebugger. Even thought I have not yet had the time to test it, it really seems the most similar way to debug the way we're used in ipython with ipdb
我刚刚发现PixieDebugger。还以为我还没来得及测试它,它似乎真的是调试我们在ipython中使用ipdb使用的方式最相似的方式
It also has an "evaluate" tab
它还有一个“评估”选项卡
回答by fuglede
A native debugger is being made available as an extension to JupyterLab. Released a few weeks ago, this can be installed by getting the relevant extension, as well as xeus-python kernel (which notably comes without the magics well-known to ipykernel users):
本机调试器正在作为 JupyterLab 的扩展提供。几周前发布,可以通过获取相关扩展以及 xeus-python 内核(特别是没有 ipykernel 用户熟知的魔法)来安装:
jupyter labextension install @jupyterlab/debugger
conda install xeus-python -c conda-forge
This enables a visual debugging experience well-known from other IDEs.
这实现了从其他 IDE 中广为人知的可视化调试体验。
Source: A visual debugger for Jupyter