简体   繁体   English

Python 调试 - Spyder - 执行期间暂停

[英]Python debugging - Spyder - Pause during execution

My python code is taking longer than expected to run.我的 python 代码运行时间比预期的要长。 I am using spyder 5.2 and python 3.9.我正在使用 spyder 5.2 和 python 3.9。 Is there a way to pause execution arbitrarily to check the line that is currently running and examine the variable explorer?有没有办法任意暂停执行以检查当前正在运行的行并检查变量资源管理器? I would like the variable explorer to show local variables within a function if a function is running at the time.如果函数当时正在运行,我希望变量资源管理器在函数中显示局部变量。

After checking the code, I would like to restart the code from the point it had stopped.检查代码后,我想从它停止的地方重新启动代码。

Notice that I am not referring to setting breakpoints before the code is run.请注意,我并不是指在代码运行之前设置断点。 Rather, what I want is to be able to pause the code at will, during execution.相反,我想要的是能够在执行期间随意暂停代码。

( Spyder maintainer here ) The only way I know of to pause execution anywhere in your code is to write the command breakpoint() in the line before the one that's giving you troubles. 这里是 Spyder 维护者)我所知道的在代码中任何地方暂停执行的唯一方法是在给您带来麻烦的行之前的行中编写命令breakpoint()

To resume execution afterwards, please write in the IPdb prompt the command !continue .之后要恢复执行,请在IPdb提示符中写入命令!continue

Note : You can add as many breakpoint() commands in your code as you want.注意:您可以根据需要在代码中添加任意数量的breakpoint()命令。 Your code will jump from one to the other after resuming execution.恢复执行后,您的代码将从一个跳转到另一个。

Yeah, you can do this in PyCharm.是的,你可以在 PyCharm 中做到这一点。 However, it would be wise to check out the documentation on the pdb .但是,最好查看pdb上的文档。 https://docs.python.org/3/library/pdb.html https://docs.python.org/3/library/pdb.html

Usually as in other languages we use print() to debug.通常与其他语言一样,我们使用print()进行调试。 However, this doesn't work in every situation.但是,这并不适用于所有情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM