逐步调试所选的Python代码 [英] Stepwise debugging of selected Python code

查看:219
本文介绍了逐步调试所选的Python代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

精华:

使用Spyder,可以通过突出显示并点击来运行部分代码 F9 。这也适用于For循环,但问题(至少对我来说)似乎不可能一步一步地运行所选部分 。我发现这有点奇怪,因为它可以在其他情况下逐步运行For Loops(并检查每一步的变量状态)。

Using Spyder, it's possible to run parts of code by highlighting it and clicking F9. This works with For Loops as well, but the problem (for me at least) is that it seems impossible to run the selected part step by step. I find this a bit strange since it's possible to run For Loops step by step under other cirmustances (and inspect the state of the variables at each step).

请允许我解释一下:

下面的代码段是对两个变量的操作使用For循环,names = ['A','B','C'] values = [11,12,13] 。为了正确描述我无法使用此设置,我觉得有必要解释一下我做什么,以及为什么我认为如果事实证明我想做的事实际上是不可能的,那么整个事情有点奇怪。

The snippet below is an operation on two variables names = ['A', 'B', 'C'] and values = [11,12,13] using a For Loop. In order to give a proper description of what I'm unable to do with this setup, I feel it's necessary to explain what I am able to do and also why I think the whole thing is a bit strange if it turns out that what I would like to do is in fact impossible.

我经常发现自己在使用不同的例程搞乱很多变量。我面临的问题是,为了调试这些例程,我经常需要重新运行部分代码,重新定义我的所有变量,然后才真正能够在代码的其他部分查找错误。这就是为什么能够在Spyders Variable explorer中检查预先存在的变量上逐步调试For循环的原因。我主要是在Spyder中使用Python,但请建议其他可能的IDE。

I often find myself messing around with a lot of variables using different routines. The problem I'm facing is that in order to debug these routines, I often have to re-run parts of the code that redefine all my variables before I'm actually able to look for bugs in other parts of the code. And THAT'S why it would be great to be able run a stepwise debugging of a For Loop on pre-existing variables that I can inspect in Spyders Variable explorer. I'm mostly messing around with Python in Spyder, but please suggest other IDEs where this may be possible.

以下是一个例子:

def foo():
    names = ['A', 'B', 'C']
    values = [11,12,13]
    i = 0
    for n in names:
        variable = str(n)  + ' = ' + str(values[i])
        print(variable)
        i += 1      
foo()






以下是我的选择(如果问题看起来很清楚,你可以轻松跳到最后一部分选项5 ):

选项1 - 使用运行代码运行文件(F5)

这不足为奇地在IPython控制台中打印以下内容:

This unsurprisingly prints the following in the IPython console:

A = 11
B = 12
C = 13

其余环境如下所示。请注意,它不会在变量资源管理器中将任何变量留在内存中和/或进行检查。这对于内存管理很有用,但对于那些不关心这些事情的小规模程序员来说是不好的。

The rest of the environment looks like below. Notice that it doesn't leave any variables in memory and/or to be inspected in the Variable explorer. That's good for memory management, but bad for a small scale wanna-be programmer that doesn't care about these things.

选项2 - 忘记功能,只需运行文件

评论out def foo() foo()并单击运行文件(F5)(还修复了缩进)将相同的东西打印到IPython控制台并将变量留在变量浏览器中以供进一步检查。

Commenting out def foo() and foo() and clicking Run file (F5) (also fixing the indentation) prints the same thing to the IPython console AND leaves the variables behind in the Variable explorer for further inspection.

编辑后的代码:

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

i = 0
for n in names:
    variable = str(n)  + ' = ' + str(values[i])
    print(variable)
    i += 1      
#foo()

环境:

请注意,变量资源管理器在执行代码后显示每个变量的状态。

Notice that the Variable explorer shows the state of each variable after the execution of the code.

选项3 - 与选项2相同的代码现在使用调试文件(Cltr + F5)运行当前行(Ctrl + F10)

Option 3 - Same code as option 2 now using Debug File (Cltr+F5) and Run Current Line (Ctrl+F10)

单击调试文件(Cltr + F5)将让我从头开始例程,并使用运行当前行逐行逐步执行( CTRL + F10)。包括For循环中的行。在每个步骤中,我可以在变量资源管理器中更改变量状态时轻松检查。

Clicking Debug File (Cltr+F5) will let me start at the beginning of the routine, and step through it line by line using Run Current Line (Ctrl+f10). Including the lines in the For Loop. At each step, I can easily inspect the state of the variables as they change in the Variable explorer.

逐步执行For循环后的环境:

非像我这样的专业程序员,这看起来像是编写和调试代码的最简单,最基本的方式。但是,很难避免将代码包装到函数中以获得更高的可重用性。所以,再次回到 foo()

To a non-professional programmer like myself, this would look like the easiest and most basic way of both writing and debugging code. However, It's hard to avoid the benefits of wrapping code into functions for greater re-useability. So, back to foo() again:

选项4 - 使用 Debug调试函数文件(Cltr + F5)继续执行直到下一个断点(Ctrl + F12)进入当前行的功能或方法(Ctrl + F11)运行当前行(Ctrl + F10)

Option 4 - Debug function with Debug File (Cltr+F5), Continue Execution Until Next Breakpoint (Ctrl+F12) and Step Into Function or Method of current line (Ctrl+F11) and Run Current Line (Ctrl+F10).

使用调试文件(Cltr + F5)启动调试并突出显示第2行。要进行,我可以使用继续执行直到下一个断点(Ctrl + F12),这将停止第11行的执行。这里我可以选择运行当前行(Ctrl + f10)调用函数而不再使用模糊,或者我可以单步执行当前行的函数或方法(Ctrl + F11),它将我带到了我快乐的地方。现在我可以运行当前行(Ctrl + F10)来定义变量,甚至可以在变量浏览器中逐步检查For循环中的变量。这是在函数中定义变量并逐步执行For循环后的环境:

Using Debug File (Cltr+F5) initiates the debugging and highlights line 2. To progress, I can use Continue Execution Until Next Breakpoint (Ctrl+F12) which halts the execution at line 11. Here I can choose to Run Current Line (Ctrl+f10) which calls the function without any more fuzz, or I can Step Into Function or Method of current line (Ctrl+F11) which brings me to my happy place. Now I can Run Current Line (Ctrl+F10) to define the variables, and even inspect the variables in the For Loop step by step in the Variable explorer. Here's the environment after defining the variables within the function and stepping through the For Loop one time:

选项5 - 调试a for使用现有变量进行循环

在Spyder中,只需突出显示代码并使用运行选择或当前行(F9)甚至没有定义函数。因此,从头开始,我只需选择它们并单击 F9 即可定义我的变量。现在,他们可以在Variable explorer中进行进一步检查:

In Spyder, you can run parts of code simply by highlighting it and using Run Selection or Current Line (F9) without even defining the function. So starting from scratch, I can define my variables simply by selecting them and clicking F9. Now they're available in the Variable explorer for further inspection:

您可以对其余代码执行相同的操作。 For Loop 完整地运行 ,并且变量在变量资源管理器中可用。

You can do the same thing with the rest of the code. The For Loop is run in its entirety and the variables become available in the Variable explorer.

现在,最后,我们得出主要问题。是否可以以任何方式逐步完成突出显示的For循环而不是同时完成整个事情,如选项1 ? (并且无需执行该功能,在断点处停止并继续使用选项4 中描述的 F10 ?)

And now, finally, we arrive at the main question. Is it in any way possible to run through the highlighted For Loop step by step instead of the whole thing at the same time like in Option 1? (And without having to run through the function, stop at a breakpoint and continue using F10 like described in Option 4?)

理想情况下,它就像突出显示代码一样简单,并执行类似逐步调试所选代码(Ctrl'??)的事情,就是这样。

Ideally, it would be as simple as highlighting the code and do something like Stepwise debug selected code (Ctrl'??) and that's it.

我希望这对你们有些人有意义。如果这种方法从头到尾完全错误,请告诉我!

I hope this makes sense to some of you. And please let me know if this approach is just completely wrong from start to finish!

谢谢!

编辑:在使用 F9 运行所选部件之前添加断点无效:

Adding a breakpoint before running the selected part with F9 has no effect:

编辑2 - 系统信息:


  • Windows 7

  • Python 3.6.0 Anaconda自定义(64位)

  • Spyder 3.2.5

推荐答案

Spyder维护者在这里)有一个技巧并不是很多人都知道的在Python代码中的任何地方引入断点: import pdb; pdb.set_trace()

(Spyder maintainer here) There is a trick not many people know to introduce breakpoints wherever you want in your Python code: import pdb; pdb.set_trace().

在您的情况下,您需要用

In your case, you need to replace your code with

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()

然后用<$ c运行你的文件$ c>运行文件 / F5 或者也可以作为 Ctrl + Enter 的单元格。通过这个简单的更改,您将获得一个完全在 pdb.set_trace()行的调试器会话,并为 c>在它下面循环以逐行迭代。

and then run your file with Run file/F5 or also as a cell with Ctrl + Enter. With this simple change you'll get a debugger session exactly at the line pdb.set_trace() is placed and enter the for cycle below it to iterate line by line through it.

注意:因为Python 3.7 (将是在2018年底发布)这将更简单,因为它将添加一个新的内置函数,名为 breakpoint 来替换 pdb.set_trace(),但效果完全相同。

Note: Since Python 3.7 (to be released at the end of 2018) this will be even simpler because it will add a new builtin function called breakpoint to replace pdb.set_trace(), but with the exact same effect.

这篇关于逐步调试所选的Python代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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