如何检查是否在脚本中设置了 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
How do I check if the python debug option is set from within a script
提问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 -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?
解析器调试模式通过-d
命令行选项或 PYTHONDEBUG 环境变量启用,从 python 2.6 开始反映在sys.flags.debug
. 但是你确定这就是你要找的吗?
回答by John La Rooy
you can use python -O
with the __debug__
variable
您可以python -O
与__debug__
变量一起使用
where -O
means optimise. so __debug__
is false
其中-O
意味着优化。所以__debug__
是假的
-d
turns on debugging for the parser, which is not what you want
-d
打开解析器的调试,这不是你想要的