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

后端 未结 8 1927
清酒与你
清酒与你 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条回答
  •  旧时难觅i
    2020-12-24 12:57

    Another way to do it hinges on how your python interpreter is started. It requires you start Python using -O for production and with no -O for debugging. So it does require an external discipline that might be hard to maintain .. but then again it might fit your processes perfectly.

    From the python docs (see "Built-in Constants" here or here):

    __debug__
    This constant is true if Python was not started with an -O option.
    

    Usage would be something like:

    if __debug__:
        print 'Python started without optimization'
    

提交回复
热议问题