iPython 调试器没有提供任何见解

iPython debugger not providing any insight

假设我有以下功能:

def simple_func():
    a = 4
    b = 5
    c = a * b
    print c

这是我在 运行 %debug simple_func():

时得到的结果
NOTE: Enter 'c' at the ipdb>  prompt to continue execution.
None
> <string>(1)<module>()

ipdb>

如果我输入 n 调试器向我吐出 20 并且 returns None.

这是跨函数、解释器、机器等发生的事情的简化版本。发生了什么事?为什么我不能让我的任何调试器做我想做的事,而我只需要做一些非常简单的逐行单步执行?

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.