python - 如何禁用 Python 警告?

标签 python suppress-warnings

我正在处理使用 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/

相关文章:

python - celery 组作为回调

python - 如何在 QTableWidget 中使用过滤选项

c++ - Clang:表达式结果未与三元运算符一起使用

objective-c - 在 Xcode 6 中关闭指定初始化程序检查

intellij-idea - 如何在 IntelliJ 的 sonarlint 插件中禁用 todo 警告?

python - 使用 gdalwarp 重新采样导致 IndentationError : unexpected indent

python - 使用另一个数据框中匹配索引的值设置数据框列

delphi - 如何暂时禁用 "return value might be undefined"警告?

python - 使用 python.multiprocessing,如何在当前进程中创建代理以传递给其他进程?

Java:抑制警告 "X is marked unstable"