在iPython笔记本中调试的正确方法是什么? [英] What is the right way to debug in iPython notebook?
问题描述
据我所知,%debug magic可以在一个单元格内进行调试。
As I know, %debug magic can do debug within one cell.
但是,我有多个单元格的函数调用。
However, I have function calls across multiple cells.
例如,
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
我尝试过:
-
我试图在cell-1的第一行设置%debug。但它甚至在执行cell-2之前立即进入调试模式。
I tried to set %debug in the first line of cell-1. But it enter into debug mode immediately, even before executing cell-2.
我尝试在代码return do_some_thing_about(b)之前的行中添加%debug。但是代码永远运行,永不停止。
I tried to add %debug in the line right before the code "return do_some_thing_about(b)". But then the code run forever, never stopped.
在ipython笔记本中设置断点的正确方法是什么?
What is the right way to set a break point within ipython notebook?
推荐答案
使用 ipdb
通过
pip install ipdb
用法:
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)
为了逐行执行 n 并使用 s 进入函数并执行退出调试提示使用 c 。
For executing line by line use n and for step into a function use s and to exit from debugging prompt use c.
有关可用命令的完整列表: https://appletree.or.kr/quick_reference_cards/Python/Python%20Debugger%20Cheatsheet.pdf
For complete list of available commands: https://appletree.or.kr/quick_reference_cards/Python/Python%20Debugger%20Cheatsheet.pdf
这篇关于在iPython笔记本中调试的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!