警告模块在 Python 2.7 中不工作?

Warnings module not working in Python 2.7?

我无法让警告模块在 Python 2.7 中工作 在用户定义的函数中,我有:

warnings.simplefilter('always', UserWarning) #added after wouldn't work 
warnings.warn('WARNING: ....')
print "Why didn't the warning above show up?"

打印语句出现,但终端上没有警告。我也确保做导入警告。不确定发生了什么。

尝试将此调用置于 warnings.warn('WARNING: ....') 下:sys.stderr.flush()

如果您查看 warnings 模块文档,您会发现:"The determination whether to issue a warning message is controlled by the warning filter, which is a sequence of matching rules and actions." 可能是过滤器问题。先尝试刷新 stderr,看看会发生什么。