Python: How to detect debug interpreter

前端 未结 3 1251
悲&欢浪女
悲&欢浪女 2021-01-04 09:09

How can I detect in my python script if its being run by the debug interpreter (ie python_d.exe rather than python.exe)? I need to change the paths to some dlls that I pass

相关标签:
3条回答
  • 2021-01-04 09:24

    Distutils use sys.gettotalrefcount to detect a debug python build:

    # ...
    if hasattr(sys, 'gettotalrefcount'):
       plat_specifier += '-pydebug'
    
    • this method doesn't rely on an executable name '*_d.exe'. It works for any name.
    • this method is cross-platform. It doesn't depend on '_d.pyd' suffix.

    See Debugging Builds and Misc/SpecialBuilds.txt

    0 讨论(0)
  • 2021-01-04 09:25

    Better, because it also works when you are running an embedded Python interpreter is to check the return value of

    imp.get_suffixes()
    

    For a debug build it contains a tuple starting with '_d.pyd':

    # debug build:
    [('_d.pyd', 'rb', 3), ('.py', 'U', 1), ('.pyw', 'U', 1), ('.pyc', 'rb', 2)]
    
    # release build:
    [('.pyd', 'rb', 3), ('.py', 'U', 1), ('.pyw', 'U', 1), ('.pyc', 'rb', 2)]
    
    0 讨论(0)
  • 2021-01-04 09:34

    An easy way, if you don't mind relying on the file name:

    if sys.executable.endswith("_d.exe"):
      print "running on debug interpreter"
    

    You can read more about the sys module and its various facilities here.

    0 讨论(0)
提交回复
热议问题