我正在开发一个相当简单的 Pylons 0.9.7 应用程序。在代码中如何判断是否启用了调试?也就是说,我对 INI 文件中 [app:main] 下的 debug 设置的值感兴趣。更一般地说,如何在代码中访问其他值?
最佳答案
# tmp.py
print __debug__
$ python tmp.py
True
$ python -O tmp.py
False
我不确定这在 Pylons 中是否成立,因为我从未使用过它 - 但在“普通”命令行 Python 中,如果未启用优化,则启用调试。 -O
标志指示 Python 打开优化。
事实上,这个片段来自 Pylons documentation :
# Display error documents for 401, 403, 404 status codes (and
# 500 when debug is disabled)
if asbool(config['debug']):
app = StatusCodeRedirect(app)
else:
app = StatusCodeRedirect(app, [400, 401, 403, 404, 500])
看起来 config['debug']
就是您想要的。
关于python - 检查 Pylons 应用程序中是否启用了调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1350227/