【问题标题】:How do I check if the python debug option is set from within a script如何检查是否从脚本中设置了 python 调试选项
【发布时间】:2010-12-08 06:35:05
【问题描述】:

如果我处于调试模式,我想做其他事情,而不是在我不处于调试模式时。

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"

然后:

python script.py
not debug mode

python -d script.py
debug mode

我怎样才能检测到它?它肯定没有使用__debug__ 变量。

【问题讨论】:

标签: python script-debugging


【解决方案1】:

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

-O 表示优化。所以__debug__ 是假的

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

【讨论】:

  • 是的,-O持续监控脚本,甚至不需要重新加载
【解决方案2】:

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

【讨论】:

  • 这适用于 2.6 及更高版本,谢谢。现在是否有人对 2.5 及以下有答案?
猜你喜欢
  • 2011-11-03
  • 2018-07-16
  • 1970-01-01
  • 2016-12-05
  • 1970-01-01
  • 1970-01-01
  • 2013-05-01
  • 2014-01-17
  • 2021-12-20
相关资源
最近更新 更多