Check if the program is running in debug mode

I am using the PyCharm IDE for Python programming.

Is there any way to check if I am in debug mode or not when I launch my profile?

I use pyplot as plt and want only the drawing to show if I am debugging my program. Yes, I can have global logical debugging that I set, but I'm looking for a sexier solution.

Thank you for your support!

+9
source share
1 answer

According to the documentation, you can use the settrace/ functions to implement the Python debugger gettrace:

sys.settrace(tracefunc) 

, Python Python. ; , settrace() .

:

CPython: settrace() , , . , , , Python.

, , - :

import sys


gettrace = getattr(sys, 'gettrace', None)

if gettrace is None:
    print('No sys.gettrace')
elif gettrace():
    print('Hmm, Big Debugger is watching me')
else:
    print("Let do something interesting")
    print(1 / 0)

pdb:

$ python -m pdb main.py 
> /home/soon/Src/Python/main/main.py(3)<module>()
-> import sys
(Pdb) step
> /home/soon/Src/Python/main/main.py(6)<module>()
-> gettrace = getattr(sys, 'gettrace', None)
(Pdb) step
> /home/soon/Src/Python/main/main.py(8)<module>()
-> if gettrace is None:
(Pdb) step
> /home/soon/Src/Python/main/main.py(10)<module>()
-> elif gettrace():
(Pdb) step
> /home/soon/Src/Python/main/main.py(11)<module>()
-> print('Hmm, Big Debugger is watching me')
(Pdb) step
Hmm, Big Debugger is watching me
--Return--
> /home/soon/Src/Python/main/main.py(11)<module>()->None
-> print('Hmm, Big Debugger is watching me')

PyCharm:

/usr/bin/python3 /opt/pycharm-professional/helpers/pydev/pydevd.py --multiproc --qt-support --client 127.0.0.1 --port 34192 --file /home/soon/Src/Python/main/main.py
pydev debugger: process 17250 is connecting

Connected to pydev debugger (build 143.1559)
Hmm, Big Debugger is watching me

Process finished with exit code 0
+11
source

Source: https://habr.com/ru/post/1649391/


All Articles