Python,如何启用所有警告?

时间:2015-04-21 23:19:27

标签: python python-2.7 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
    )

3 个答案:

答案 0 :(得分:13)

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

答案 1 :(得分:8)

import warnings
warnings.simplefilter('module')

或者:

import warnings
warnings.simplefilter('always')

过滤器列表位于docs

答案 2 :(得分:1)

您还可以仅对一段代码启用警告:

      word        start  stop speaker
0                but,  2.72  2.85  2
1      that's alright  2.86  3.47  2
2       we'll have to  8.43  9.07  1
3               okay!  9.19 10.01  2
4                sure  10.02 11.01 2
5               what? 11.02 12.00  1
6            I agree, 12.01 14.00  2
7      but i disagree 14.01 17.00  2
8          thats fine 17.01 19.00  1
9     however you are 19.01 22.00  1
10          like this 22.01 24.00  1
11                and 24.01 25.00  1