How do I check if the python debug option is set from within a script

做~自己de王妃 提交于 2019-11-30 03:10:18

问题


If I'm in debug mode, I want to do other stuff than when I'm not.

if DEBUG:
    STORED_DATA_FILE = os.path.join(TEMP_DIR, 'store.dat')
    LOG_LEVEL = logging.DEBUG
    print "debug mode"
else:
    STORED_DATA_FILE = os.path.join(SCRIPT_PATH, 'store.dat')
    LOG_LEVEL = logging.INFO
    print "not debug mode"

then:

python script.py
not debug mode

python -d script.py
debug mode

How can I detect that? It certainly isn't using the __debug__ variable.


回答1:


Parser debug mode is enabled with -d commandline option or PYTHONDEBUG environment variable and starting from python 2.6 is reflected in sys.flags.debug. But are you sure this is what you are looking for?




回答2:


you can use python -O with the __debug__ variable

where -O means optimise. so __debug__ is false

-d turns on debugging for the parser, which is not what you want



来源:https://stackoverflow.com/questions/1593274/how-do-i-check-if-the-python-debug-option-is-set-from-within-a-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!