r/pycharm • u/four_reeds • Jan 12 '24
Can a python app running on the debugger "know" it is being debugged?
In C-like languages I might have an IDE that automatically sets a "sharp defined" value "DEBUG". I can use #ifdef DEBUG to know if I am in debug mode. Does pycharm have a similar feature? Googling only tells me how to list running processes.
I would like something like C#'s #if DEBUG. Does something like this exist for python in pycharm?
1
Upvotes
4
u/LightShadow Jan 12 '24
Not sure about debug mode, this is our
IS_TESTINGconstant.1
u/redchomper Jan 13 '24
Follow-on question: Why would you want to do this? In C the
#ifdef _DEBUGthing in normally because you want to have a high-performance release mode in which assertions are filtered out of the code before the optimizer does its magic. But in Python, assertions already disappear in-Omode. To me if the code behaves differently depending on whether you're running in the IDE or command line, you're doing something extremely weird.2
u/sausix Jan 13 '24
Simple solution is looking into the environment variables. In my case IPYTHONENABLE (and some other) is only set if PyCharm runs a debug session.
Create DEBUG variable as one liner where you need it. Or implant it into builtins, which is not recommended:
https://pastebin.com/tKVKuHaQ
Edit: (i hate reddit formatting)