Way for my Python code to detect if it's being run in the debugger?

Hi!  Is there a way for me to detect in my Python code if it's being run in the PyCharm debugger?  I'm using Kivy, and it has a nice "inspector" for examining object attributes; what I'd like to do is instantiate it only if I'm NOT running in debug mode.  I looked among the identifiers in what appears to be the call stack in debug mode and see some items that appear as if I could key on them, e.g., "debugger" in <module>, pydevd.py, but I don't know how to access them, if I even can.  Thanks for your help.

DLG
3 comments
Comment actions Permalink
Maybe you can adapt your program to check for a -d or –debug option, and add that option in a Run Configuration to be used in a debug session?
0
Comment actions Permalink
Hi, you could check the result of sys.gettrace() is None. That will mean that there is no PyCharm debugger involved, not another one.
1
Comment actions Permalink
Excellent, thank you Dmitry!  I love it when things work the first time as advertised!!!  And thank you, also, Roger: I actually figured out and implemented that solution on my own (but it's much more cumbersome and involved than Dmitry's one-liner) but thank you for replying nevertheless.
0

Please sign in to leave a comment.