首页 > 解决方案 > 为什么 pdb.set_trace() 不进入下一行,而是进入 interactiveshell.py?

问题描述

我正在使用 pdb.set_trace() +n 单步执行我的代码的每一行。大多数时候它都有效。我现在有一个新代码,并且在 for 循环之前设置了跟踪。当我单击“n”时,它没有进入 For 循环,而是进入了一些 interactiveshell.py 文件:

for item in init_times:
(Pdb) n
> c:\anaconda\lib\site-packages\ipython\core\interactiveshell.py(3329)run_code()
-> sys.excepthook = old_excepthook

如何摆脱这种情况并继续逐行调试我的代码?遍历这个交互式shell 需要很长时间,我不知道它是什么或者我需要在其中调试什么。

这是我在 If 语句之前有跟踪的代码快照:

for item in init_times:
    pdb.set_trace()
    if item[0]=='From Seconds':
        start_0.append(1)
    elif item[0]=='To Seconds':
        end_last.append(1)
if len(start_0)>0 and len(end_last)>0:
    full_range = 1
    print('full range')
elif len(start_0)>0 and not len(end_last)>0:
    begin_range_at_0 = 1
    print('data begins at 0')
elif not len(start_0)>0 and len(end_last)>0:
    end_range_at_end = 1
    print('data ends at last row')
else:
    min_start = int(min(t1_comp_init[0], t1_stat_init[0]))    # min_start variable is used in statistics() and comparisons() when both 
    max_end = int(max(t1_comp_init[0], t2_stat_init[0])) 

标签: pythondebuggingpdb

解决方案


您可以尝试放入pdb.set_trace()一个函数。
它会很好地工作,就像下面的代码一样:

def s():
    pdb.set_trace()
    a = "aaa"
    b = "bbb"
    c = "ccc"
    return  a + b + c 

s()

推荐阅读