在 iPython notebook 中调试的正确方法是什么? [英] What is the right way to debug in iPython notebook?
问题描述
据我所知,%debug magic
可以在一个单元格内进行调试.
但是,我有跨多个单元格的函数调用.
例如
In[1]: def fun1(a)def fun2(b)# 我想为下一行设置断点 #返回 do_some_thing_about(b)返回乐趣2(a)In[2]:将多处理导入为 mppool=mp.Pool(进程数=2)结果=pool.map(fun1, 1.0)池.close()池连接
我的尝试:
我试图在 cell-1 的第一行设置
%debug
.但它会立即进入调试模式,甚至在执行 cell-2 之前.我尝试在代码
return do_some_thing_about(b)
之前的行中添加%debug
.但是代码会永远运行,永远不会停止.
在 ipython notebook 中设置断点的正确方法是什么?
使用 ipdb
通过
安装pip install ipdb
用法:
In[1]: def fun1(a):def fun2(a):导入ipdb;ipdb.set_trace() # 调试从这里开始返回 do_some_thing_about(b)返回乐趣2(a)在[2]:fun1(1)
要逐行执行,请使用 n,要进入函数,请使用 s,要退出调试提示,请使用 c.>
可用命令的完整列表:https://appletree.or.kr/quick_reference_cards/Python/Python%20Debugger%20Cheatsheet.pdf
As I know, %debug magic
can do debug within one cell.
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.
What is the right way to set a break point within the ipython notebook?
Use 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 n and for step into a function use s and to exit from debugging prompt use c.
For complete list of available commands: https://appletree.or.kr/quick_reference_cards/Python/Python%20Debugger%20Cheatsheet.pdf
这篇关于在 iPython notebook 中调试的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!