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

发布于 2024-08-07 20:01:32 字数 458 浏览 5 评论 0原文

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

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__ 变量。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坐在坟头思考人生 2024-08-14 20:01:32

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

其中 -O 表示优化。所以 __debug__ 是 false

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

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

め可乐爱微笑 2024-08-14 20:01:32

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

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文