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

后端 未结 8 1895
清酒与你
清酒与你 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:03

    From taking a quick look at the pdb docs and source code, it doesn't look like there is a built in way to do this. I suggest that you set an environment variable that indicates debugging is in progress and have your application respond to that.

    $ USING_PDB=1 pdb yourprog.py
    

    Then in yourprog.py:

    import os
    if os.environ.get('USING_PDB'):
        # debugging actions
        pass
    

提交回复
热议问题