python - Check if programm runs in Debug mode -
i use pycharm ide python programming.
is there possibility check, whether i'm in debugging mode or not when run proframm?
i use pyplot plt , want figure shown if debug programm. yes, have global boolean debug set myself, sexier solution.
thank support!
according documentation, settrace
/ gettrace
functions used in order implement python debugger:
sys.settrace(tracefunc)
set system’s trace function, allows implement python source code debugger in python. function thread-specific; debugger support multiple threads, must registered using
settrace()
each thread being debugged.
however, these methods may not available in implementations:
cpython implementation detail:
settrace()
function intended implementing debuggers, profilers, coverage tools , like. behavior part of implementation platform, rather part of language definition, , may not available in python implementations.
you use following snippet in order check if debugging code:
import sys gettrace = getattr(sys, 'gettrace', none) if gettrace none: print('no sys.gettrace') elif gettrace(): print('hmm, big debugger watching me') else: print("let's interesting") print(1 / 0)
this 1 works 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 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 watching me') (pdb) step hmm, big debugger watching me --return-- > /home/soon/src/python/main/main.py(11)<module>()->none -> print('hmm, big debugger watching me')
and 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 connecting connected pydev debugger (build 143.1559) hmm, big debugger watching me process finished exit code 0
Comments
Post a Comment