我有以下片段:
a = np.array([1, 2, 3])
b = np.array([False, True])
print(a[b])它像预期的那样触发一个VisibleDeprecationWarning。
现在,当我导入某个模块时,不再显示该警告:
import questionable_module
a = np.array([1, 2, 3])
b = np.array([False, True])
print(a[b])如何修改代码才能重新启用所有警告?我既不想也不能改变questionable_module。如果可能的话,我更愿意在代码中而不是命令行参数中这样做。
questionable_module是Glumpy,但我正在寻找一种独立于其他模块的解决方案。
发布于 2017-05-07 15:09:28
我检查了一下,对于glumpy,他们似乎使用logging.captureWarnings来捕捉警告:
导入警告导入日志记录logging.captureWarnings(真)
我不确定他们是否打算记录所有警告,但是您可以用
import logging
logging.captureWarnings(False)其他可能性(在这种情况下不适用),但在未来可能会有所帮助:
通常情况下,也可能是他们调整了warnings.simplefilter,您可以像这样再次启用它:
import warnings
warnings.simplefilter("always", VisibleDeprecationWarning)或者用warnings.resetwarnings将其重置为默认值。
如果它实际上是一个NumPy浮点警告,那么您需要使用numpy.seterr
import numpy as np
np.seterr(all='warn')但它也可能是questionable_module 真正取代或修补函数的方式,实际上是从不达到了引发警告的程度。那样的话你可能什么也做不了。
发布于 2017-05-07 15:12:13
试试这个:
import warnings
import questionable_module
warnings.resetwarnings() # Reset the warnings filter. This discards the effect of all previous calls to filterwarnings(), including that of the -W command line options and calls to simplefilter().
warnings.simplefilter('default')
a = np.array([1, 2, 3])
b = np.array([False, True])
print(a[b])https://stackoverflow.com/questions/43832981
复制相似问题