Python,如何启用所有警告?

Python, how to enable all warnings?

我正在设置一个似乎合适的 ImportWarning,但发现默认情况下不报告此警告;

如何设置 python 报告 ImportWarning 或所有警告?

这是我写的导入警告:

try:
    from markdown import markdown

except ImportError, err:
    warnings.warn(
        'Unable to load Pypi package `markdown`, HTML output will be unavailable. {}'.format(err),
        ImportWarning
    )

使用 -Wdefault-Wd 开关启用警告 运行 python。

import warnings
warnings.simplefilter('module')

或者:

import warnings
warnings.simplefilter('always')

过滤器列表在 docs

您也可以只为一段代码启用警告:

import warnings
with warnings.catch_warnings():
    warnings.simplefilter('always')
    # all warnings here are enabled
# warnings here are left as default (probably silent)