Python:如何检测调试解释器

Fir*_*cer 12 python debugging

如何在我的python脚本中检测它是否由调试解释器运行(即python_d.exe而不是python.exe)?我需要改变传递给扩展的一些dll的路径.

例如,我喜欢在我的python脚本开头做类似的事情:

#get paths to graphics dlls
if debug_build:
    d3d9Path   = "bin\\debug\\direct3d9.dll"
    d3d10Path  = "bin\\debug\\direct3d10.dll"
    openGLPath = "bin\\debug\\openGL2.dll"
else:
    d3d9Path   = "bin\\direct3d9.dll"
    d3d10Path  = "bin\\direct3d10.dll"
    openGLPath = "bin\\openGL2.dll"
Run Code Online (Sandbox Code Playgroud)

我想在扩展中添加一个"IsDebug()"方法,如果它是调试版本(即使用"#define DEBUG"构建),则返回true,否则返回false.但这似乎是一个黑客的东西我确定我可以让python告诉我...

jfs*_*jfs 14

Distutils用于sys.gettotalrefcount检测调试python构建:

# ...
if hasattr(sys, 'gettotalrefcount'):
   plat_specifier += '-pydebug'
Run Code Online (Sandbox Code Playgroud)
  • 此方法不依赖于可执行文件名' *_d.exe'.它适用于任何名称.
  • 这种方法是跨平台的.它不依赖于' _d.pyd'后缀.

请参阅调试构建Misc/SpecialBuilds.txt

  • 这是一个旧答案,但我是一个来自未来的人,以确认这在 Python 3.7.1 上仍然可以正常工作。 (2认同)