我正在研究一个相当简单的Pylons 0.9.7应用程序。如何在代码中告知是否启用了调试?也就是说,我对INI文件中 [app:main] 下的调试设置的价值感兴趣。更一般地说,如何从我的代码中访问其他值?
答案 0 :(得分:3)
# 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']
就是你想要的。