If you are using code that you know will raise a warning, such as a deprecated function, but do not want to see the warning, then it is possible to suppress the warning using the catch_warnings context manager:
import warnings
def fxn():
warnings.warn("deprecated", DeprecationWarning)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
fxn()
我不宽恕,但您可以用以下命令来抑制所有警告:
import warnings
warnings.filterwarnings("ignore")
例如:
>>> import warnings
>>> def f():
... print('before')
... warnings.warn('you are warned!')
... print('after')
>>> f()
before
__main__:3: UserWarning: you are warned!
after
>>> warnings.filterwarnings("ignore")
>>> f()
before
after
你看过python文档的suppress warnings部分吗?
我不宽恕,但您可以用以下命令来抑制所有警告:
例如:
您还可以定义一个环境变量(2010年的新特性,即python 2.7)
测试如下:默认值
忽略警告
对于不推荐警告,请查看how-to-ignore-deprecation-warnings-in-python
复制到这里。。。
从^{} module 的文档中:
如果您在Windows上,请将
-W ignore::DeprecationWarning
作为参数传递给Python。不过最好是通过强制转换到int来解决问题。(注意,在Python3.2中,默认情况下会忽略弃用警告。)
或:
现在您仍然可以得到所有其他的
DeprecationWarning
s,但不是由以下原因引起的:这是-W option。
python -W ignore foo.py
相关问题 更多 >
编程相关推荐