如何检查是否在脚本中设置了 python 调试选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1593274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:39:00  来源:igfitidea点击:

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

pythonscript-debugging

提问by user132262

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.

我怎么能检测到呢?它当然没有使用__debug__变量。

采纳答案by Denis Otkidach

Parser debug mode is enabled with -dcommandline 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?

解析器调试模式通过-d命令行选项或 PYTHONDEBUG 环境变量启用,从 python 2.6 开始反映在sys.flags.debug. 但是你确定这就是你要找的吗?

回答by John La Rooy

you can use python -Owith the __debug__variable

您可以python -O__debug__变量一起使用

where -Omeans optimise. so __debug__is false

其中-O意味着优化。所以__debug__是假的

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

-d打开解析器的调试,这不是你想要的