python - 暂停/恢复脚本执行?

标签 python

我正在尝试找到一种方法来暂停脚本的执行(一个没有超时的方法),这将允许我,例如此时检查变量值,从命令行输入其他命令,然后在我准备好后恢复执行。在 IDL(最近我从 IDL 切换到 Python)中,我能够通过在所选代码行包含“stop”然后键入“.cont”来做到这一点。 Python 中是否存在类似的东西?

非常感谢您的帮助!

(这是我在这里的第一篇文章,所以我希望以上描述符合标准,如果不符合标准,欢迎任何批评意见!)

最佳答案

我相信您正在寻找the Python Debugger (pdb) :

The module pdb defines an interactive source code debugger for Python programs. It supports setting (conditional) breakpoints and single stepping at the source line level, inspection of stack frames, source code listing, and evaluation of arbitrary Python code in the context of any stack frame. It also supports post-mortem debugging and can be called under program control.

我建议您从 set_trace() 开始:

The typical usage to break into the debugger from a running program is to insert

import pdb; pdb.set_trace()

at the location you want to break into the debugger. You can then step through the code following this statement, and continue running without the debugger using the continue command.

一定要看看the other ways to enter the DebuggerDebugger Commands部分。

关于python - 暂停/恢复脚本执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50423272/

相关文章:

Python 在 MacOS M1 上导入 MySQLdb 不起作用

python - 为什么 C++ 代码实现的性能并不比 Python 实现更好?

python - 使用 lambda 进行复杂排序

python - 标签文本未显示 'á'(匈牙利语)字母

python - Ubuntu 上的 PyQt4 安装问题

python - 如何将 pandas DataFrame 中的列取消嵌套(分解)为多行

python - 当 NaN 包含在索引中时,如何有效地删除?

Python : go backwards through a string, 然后删除特定字符后的所有内容

python - numpy:将(an,)数组转换为(n,1)数组的语法/习惯用法?

python - 如何处理Python中Spearman测试中的nan_policy错误