为什么导入pdb;在Spyder中以不同方式调用时,pdb.set_trace会触发两种不同的调试方案吗? [英] Why does import pdb; pdb.set_trace trigger two different debugging scenarios when called differently in Spyder?

查看:198
本文介绍了为什么导入pdb;在Spyder中以不同方式调用时,pdb.set_trace会触发两种不同的调试方案吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是



继续执行继续执行直到下一个断点(F12)直接到达第8行。另请注意,在下面的屏幕截图中,变量资源管理器填充了变量 i 姓名。当您使用运行当前行(F10)时,添加和更新其他变量:





或者你可以继续执行直到下一个断点(F12)并以这种方式完成程序。将前者一直做到 foo()清除变量资源管理器,在ipdb调试器中打印--Return--并退出调试器。



我想我应该每次都采用这种方式做事,但我对Spyder提供的运行代码的其他选项非常感兴趣。而且我特别喜欢使用#%%和Ctrl + Enter来定义和运行单元格。



场景2 - Ctrl + Enter在包含完整代码



Ctrl + Enter在单元格高亮显示第8行,并填充变量资源管理器:





继续执行继续执行直至下一个断点(F12)清除变量资源管理器并像以前一样退出调试器:





这也很好,但这是我的理由:



场景3 - 运行和调试多个单元格



当我调试片段时o对于更大的数据科学项目的代码,我经常最终在一个地方定义一些变量,并希望调试在其他地方使用这些变量作为输入的函数。这就是为什么我经常在下面的情况下结束,我在一个单元格中定义了变量,并在另一个单元格中使用相同的变量进行For循环:



包含变量的单元格





包含For循环的单元格





但按Ctrl + Enter并继续运行当前行(F10)会触发 interactiveshell.py 中的混乱情况:





现在,问题是:


  1. 这里发生了什么?

  2. 这可以避免吗?

  3. 为什么不能调试单元格(或突出显示的代码+ f9)之类的这个?

感谢您的任何建议!

解决方案

Spyder维护者在这里)我认为问题是你正在尝试评估函数范围内的单元格。在这种情况下,所有变量都是作用域的本地变量,因此您无法在片段中评估 foo ,这就是您尝试对我们的单元格进行的操作。



为了达到你想要的效果,你可以改用一个类。这将允许您将数据保存在类中所有方法共享的变量中,并定义要操作这些数据的函数/方法。一个类还允许您整齐地封装数据和函数/方法,即不在代码的全局范围内定义。



有了这个,你可以简单地运行调用要调试的方法的单元格。在上面发布的示例中,这将是

 #%% 
class Foo:
def __init__ (个体经营):
self.names = ['A','B','C']
self.values = [11,12,13]

def操纵(个体经营):
i = 0
import pdb; pdb.set_trace()
表示self.names中的n:
variable = str(n)+'='+ str(self.values [i])
print(variable)
i + = 1

f = Foo()

#%%
f.manipulation()

这使我可以毫无问题地调试操作方法的工作方式。


This is a follow-up question to Stepwise debugging of selected Python code.

Why does import pdb; pdb.set_trace trigger two different debugging scenarios when called differently in Spyder?

Here's the edited sample code in the answer from Carlos Cordoba to the question mentioned above.

The code:

def foo():
    names = ['A', 'B', 'C']
    values = [11,12,13]

    i = 0
    import pdb; pdb.set_trace()
    for n in names:
        variable = str(n)  + ' = ' + str(values[i])
        print(variable)
        i += 1      
foo()

Scenario1 - Run file (F5), Continue Execution until next breakpoint (F12) and Run Current Line (F10)

This procedure works just fine. Let me just explain how for the sake of context:

Run file (F5) highlights line 2:

Proceeding with Continue Execution until next breakpoint (F12) takes you directly to line 8. Also notice in the screenshot below that the Variable explorer is populated with the variables i, names and values. Other variables are added and updated as you go through the rest of the code with Run Current Line (F10):

Or you could Continue Execution until next breakpoint (F12) and finish the procedure that way. Doing the former all the way down to foo() clears the variable explorer, prints --Return-- in the ipdb debugger, and exits the debugger.

And I guess I should just go with this way of doing things every time, but I'm very interested in the other options of running code that Spyder offers as well. And I'm particularly fond of defining and running cells with #%% and Ctrl+Enter.

Scenario 2 - Ctrl+Enter in a cell that includes the entire code

Ctrl+Enter within the cell highlights line 8, and populates the Variable Explorer:

Proceeding with Continue Execution until next breakpoint (F12) clears the variable explorer and exits the debugger just like before:

And that's fine too, but here's my case:

Scenario 3 - Running and debugging multiple cells

When I'm debugging fragments of code of bigger data science projects, I often end up defining some variables one place, and wanting to debug functions that use these variables as input somewhere else. And that's why I often end up in the situation below, where I've defined the variables in one cell, and have a For Loop using the same variables in another cell:

Cell that contains variables

Cell that contains For Loop

But hitting Ctrl+Enter and proceeding with Run Current Line (F10) triggers a messy situation within interactiveshell.py:

And now, the questions:

  1. What's going on here?
  2. Can this be avoided?
  3. Why isn't it possible to debug cells (or highlighted code + f9) like this?

Thank you for any suggestions!

解决方案

(Spyder maintainer here) I think the problem is you're trying to evaluate cells inside the scope of a function. In this case all variables are local to the scope, so you can't evaluate foo in pieces, which is what you're trying to do with our cells.

To achieve what you want, you could use a class instead. That would allow you to save your data in variables shared by all methods in the class and to define the functions/methods you want to manipulate those data. A class would also allow you to have data and functions/methods neatly encapsulated, i.e. without being defined in the global scope of your code.

With that, you could simply run a cell that calls the method you want to debug. In the example you posted above, this will be

# %%
class Foo:
    def __init__(self):
        self.names = ['A', 'B', 'C']
        self.values = [11,12,13]

    def manipulation(self):
        i = 0
        import pdb; pdb.set_trace()
        for n in self.names:
            variable = str(n)  + ' = ' + str(self.values[i])
            print(variable)
            i += 1      

f = Foo()

# %%
f.manipulation()

This allows me to debug the workings of the manipulation method without problems.

这篇关于为什么导入pdb;在Spyder中以不同方式调用时,pdb.set_trace会触发两种不同的调试方案吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆