它看起来不适用于在会话debug
中简单定义的函数。ipython
它需要从文件中导入(即--breakpoint
参数采用文件名和行)。
如果我创建一个文件test.py
In [9]: cat test.py
def simple_func():
a = 4
b = 5
c = a * b
print(c)
我可以:
In [10]: import test
In [11]: %debug --breakpoint test.py:1 test.simple_func()
Breakpoint 1 at /home/paul/mypy/test.py:1
NOTE: Enter 'c' at the ipdb> prompt to continue execution.
> /home/paul/mypy/test.py(2)simple_func()
1 1 def simple_func():
----> 2 a = 4
3 b = 5
4 c = a * b
5 print(c)
ipdb> n
> /home/paul/mypy/test.py(3)simple_func()
1 1 def simple_func():
2 a = 4
----> 3 b = 5
4 c = a * b
5 print(c)
ipdb> n
> /home/paul/mypy/test.py(4)simple_func()
2 a = 4
3 b = 5
----> 4 c = a * b
5 print(c)
6
ipdb> a,b
(4, 5)
ipdb> n
> /home/paul/mypy/test.py(5)simple_func()
2 a = 4
3 b = 5
4 c = a * b
----> 5 print(c)
6
ipdb> c
20
ipdb> c
20
ipdb> q
可能还有其他使用方法,但这似乎是最简单、最直接的一种。我很少使用调试器。相反,我在 Ipython 中以交互方式测试代码片段,并在我的脚本中添加调试prints
。