配置IPython以始终显示警告

时间:2015-01-05 20:50:59

标签: python ipython

我第一次做一些在IPython shell中发出警告的东西,我看到了。但是后来我没有。例如,

In [1]: import numpy as np
In [2]: np.uint8(250) * np.uint8(2)
/Users/me/anaconda/envs/py33/bin/ipython:1: RuntimeWarning: overflow encountered in ubyte_scalars
#!/bin/bash /Users/me/anaconda/envs/py33/bin/python.app
Out[2]: 244

In [3]: np.uint8(250) * np.uint8(2)
Out[3]: 244       # No warning!

如何配置IPython以始终显示警告?我试过了:

import warnings
warnings.filterwarnings('always')

但这并没有任何区别。

1 个答案:

答案 0 :(得分:3)

我认为这是由IPython团队相对recently解决的。由于有点不同寻常的设计决定,因此warnings并没有很好地发挥作用。在普通的Python中,always上的图腾就足够了,现在如果我在IPython trunk中做同样的事情:

In [1]: import warnings

In [2]: warnings.filterwarnings('always')

In [3]: import numpy as np

In [4]: np.uint8(250) * np.uint8(2)
/home/dsm/sys/root/bin/ipython3.4:1: RuntimeWarning: overflow encountered in ubyte_scalars
  #!/home/dsm/sys/root/bin/python3.4
Out[4]: 244

In [5]: np.uint8(250) * np.uint8(2)
/home/dsm/sys/root/bin/ipython3.4:1: RuntimeWarning: overflow encountered in ubyte_scalars
  #!/home/dsm/sys/root/bin/python3.4
Out[5]: 244