How to detect that Python code is being executed through the debugger?

后端 未结 8 1880
清酒与你
清酒与你 2020-12-24 12:20

Is there a simple way to detect, within Python code, that this code is being executed through the Python debugger?

I have a small Python application that uses Java c

8条回答
  •  既然无缘
    2020-12-24 13:00

    A solution working with Python 2.4 (it should work with any version superior to 2.1) and Pydev:

    import inspect
    
    def isdebugging():
      for frame in inspect.stack():
        if frame[1].endswith("pydevd.py"):
          return True
      return False
    

    The same should work with pdb by simply replacing pydevd.py with pdb.py. As do3cc suggested, it tries to find the debugger within the stack of the caller.

    Useful links:

    • The Python Debugger
    • The interpreter stack

提交回复
热议问题