Check if program runs in Debug mode(检查程序是否在调试模式下运行)
问题描述
我使用 PyCharm IDE 进行 Python 编程.
是否有可能在我运行程序时检查我是否处于调试模式?
我使用 pyplot 作为 plt,并且只希望在调试程序时显示一个 Figure.是的,我可以有一个自己设置的全局布尔 _debug_
,但我正在寻找更好的解决方案.
根据文档,可以使用 settrace
/gettrace
函数来实现 Python 调试器:
sys.settrace(tracefunc)
设置系统的trace功能,允许您可以在 Python 中实现 Python 源代码调试器.功能是线程特定的;对于支持多线程的调试器,它必须使用 settrace()
为每个正在调试的线程注册.
但是,这些方法可能并非在所有实现中都可用:
<块引用>CPython 实现细节:settrace()
函数旨在仅用于实现调试器、分析器、覆盖工具和喜欢.它的行为是实现平台的一部分,而不是语言定义的一部分,因此可能并非全部可用Python 实现.
您可以使用以下代码段来检查是否有人在调试您的代码:
导入系统gettrace = getattr(sys, 'gettrace', 无)如果 gettrace 为无:print('没有 sys.gettrace')elif gettrace():print('嗯,大调试器在看着我')别的:print("让我们做一些有趣的事情")打印(1/0)
这个适用于 pdb:
$ python -m pdb main.py>/home/soon/Src/Python/main/main.py(3)<模块>()->导入系统(pdb) 步骤>/home/soon/Src/Python/main/main.py(6)<模块>()->gettrace = getattr(sys, 'gettrace', 无)(pdb) 步骤>/home/soon/Src/Python/main/main.py(8)<模块>()->如果 gettrace 为无:(pdb) 步骤>/home/soon/Src/Python/main/main.py(10)<模块>()->elif gettrace():(pdb) 步骤>/home/soon/Src/Python/main/main.py(11)<模块>()->print('嗯,大调试器在看着我')(pdb) 步骤嗯,大调试器在看着我- 返回 ->/home/soon/Src/Python/main/main.py(11)<module>()->无->print('嗯,大调试器在看着我')
还有 PyCharm:
/usr/bin/python3/opt/pycharm-professional/helpers/pydev/pydevd.py --multiproc --qt-support --client 127.0.0.1 --port 34192 --file/home/很快/Src/Python/main/main.pypydev 调试器:进程 17250 正在连接连接到 pydev 调试器(内部版本 143.1559)嗯,大调试器在看着我进程以退出代码 0 结束
I use the PyCharm IDE for Python programming.
Is there a possibility to check, whether I'm in debugging mode or not when I run my program?
I use pyplot as plt and want a Figure only to be shown if I debug my program. Yes, I could have a global boolean _debug_
which is set by myself, but I look for a better solution.
According to the documentation, settrace
/ gettrace
functions could be used in order to implement Python debugger:
sys.settrace(tracefunc)
Set the system’s trace function, which allows you to implement a Python source code debugger in Python. The function is thread-specific; for a debugger to support multiple threads, it must be registered using
settrace()
for each thread being debugged.
However, these methods may not be available in all implementations:
CPython implementation detail: The
settrace()
function is intended only for implementing debuggers, profilers, coverage tools and the like. Its behavior is part of the implementation platform, rather than part of the language definition, and thus may not be available in all Python implementations.
You could use the following snippet in order to check if someone is debugging your code:
import sys
gettrace = getattr(sys, 'gettrace', None)
if gettrace is None:
print('No sys.gettrace')
elif gettrace():
print('Hmm, Big Debugger is watching me')
else:
print("Let's do something interesting")
print(1 / 0)
This one works for pdb:
$ python -m pdb main.py
> /home/soon/Src/Python/main/main.py(3)<module>()
-> import sys
(Pdb) step
> /home/soon/Src/Python/main/main.py(6)<module>()
-> gettrace = getattr(sys, 'gettrace', None)
(Pdb) step
> /home/soon/Src/Python/main/main.py(8)<module>()
-> if gettrace is None:
(Pdb) step
> /home/soon/Src/Python/main/main.py(10)<module>()
-> elif gettrace():
(Pdb) step
> /home/soon/Src/Python/main/main.py(11)<module>()
-> print('Hmm, Big Debugger is watching me')
(Pdb) step
Hmm, Big Debugger is watching me
--Return--
> /home/soon/Src/Python/main/main.py(11)<module>()->None
-> print('Hmm, Big Debugger is watching me')
And PyCharm:
/usr/bin/python3 /opt/pycharm-professional/helpers/pydev/pydevd.py --multiproc --qt-support --client 127.0.0.1 --port 34192 --file /home/soon/Src/Python/main/main.py
pydev debugger: process 17250 is connecting
Connected to pydev debugger (build 143.1559)
Hmm, Big Debugger is watching me
Process finished with exit code 0
这篇关于检查程序是否在调试模式下运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:检查程序是否在调试模式下运行
基础教程推荐
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 筛选NumPy数组 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01