我正在处理使用 warnings
引发很多(对我而言)无用警告的代码。图书馆。阅读(/扫描)文档我只找到了一种方法to disable warnings for single functions .但我不想更改这么多代码。
是否有类似 python -no-warning foo.py
的标志?
你会推荐什么?
最佳答案
看看Temporarily Suppressing Warnings Python 文档部分:
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
<stdin>:3: UserWarning: you are warned!
after
>>> warnings.filterwarnings("ignore")
>>> f()
before
after
关于python - 如何禁用 Python 警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14463277/