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
DLG
3 comments
Sort by
Date
Votes
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?
Hi, you could check the result of sys.gettrace() is None. That will mean that there is no PyCharm debugger involved, not another one.
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.
Please sign in to leave a comment.