如何禁用python警告

Fra*_*ter 350 python suppress-warnings

我正在处理代码,并使用warnings库抛出了很多(对我而言)无用的警告.阅读(/扫描)文档我只发现了一种禁用单个函数警告的方法.但我不想改变这么多的代码.

可能有旗帜python -no-warning foo.py吗?

你会推荐什么?

Mik*_*ike 469

您是否看过python docs 的抑制警告部分?

如果您使用的代码会引发警告(例如已弃用的函数),但不希望看到警告,则可以使用catch_warnings上下文管理器来禁止警告:

import warnings

def fxn():
    warnings.warn("deprecated", DeprecationWarning)

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    fxn()
Run Code Online (Sandbox Code Playgroud)

我不宽恕它,但你可以用这个来压抑所有的警告:

import warnings
warnings.filterwarnings("ignore")
Run Code Online (Sandbox Code Playgroud)

例如:

>>> 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
Run Code Online (Sandbox Code Playgroud)

  • 如果您只希望捕获特定类别的警告,可以使用`category`参数传递它:`warnings.filterwarnings("ignore",category = DeprecationWarning)` (33认同)
  • @Framester - 是的,IMO这是抑制特定警告的最简洁方法,一般会出现警告,因为某些事情可能是错误的,因此通过命令行抑制所有警告可能不是最好的选择. (10认同)
  • `warnings.filterwarnings`函数还有一个有用的参数:`module`.它允许您忽略指定模块的警告. (4认同)

Pav*_*sov 343

-W选项.

python -W ignore foo.py

  • 要仅忽略特定消息,您可以在参数中添加详细信息。除了模块名称规范之外,“man python”对其进行了详细描述。我花了很多时间猜测它。最后我得到了这个命令`python3 -Wignore::UserWarning:git.cmd:914 ./test.py`用于警告`/usr/local/lib/python3.4/dist-packages/git/cmd.py:914 : UserWarning: Python 3.5 支持已弃用...` (6认同)

Hol*_*lle 77

您还可以定义一个环境变量(2010年的新功能 - 即python 2.7)

export PYTHONWARNINGS="ignore"
Run Code Online (Sandbox Code Playgroud)

像这样测试:默认

$ export PYTHONWARNINGS="default"
$ python
>>> import warnings
>>> warnings.warn('my warning')
__main__:1: UserWarning: my warning
>>>
Run Code Online (Sandbox Code Playgroud)

忽略警告

$ export PYTHONWARNINGS="ignore"
$ python
>>> import warnings
>>> warnings.warn('my warning')
>>> 
Run Code Online (Sandbox Code Playgroud)

  • 这对于执行测试时忽略警告特别有用。使用`tox`,在`setenv`中添加`PYTHONWARNINGS = ignore`可以减少输出。 (2认同)
  • 对于 AWS CLI 也非常有用。 (2认同)

Chr*_*nds 61

这是一个老问题,但在PEP 565中有一些更新的指导,如果你正在编写一个你应该使用的python应用程序来关闭所有警告:

import sys
import warnings

if not sys.warnoptions:
    warnings.simplefilter("ignore")
Run Code Online (Sandbox Code Playgroud)

建议这样做的原因是它默认关闭所有警告,但关键是允许它们通过python -W命令行或者重新打开PYTHONWARNINGS.


Abh*_*ain 41

如果您不想要复杂的东西,那么:

import warnings
warnings.filterwarnings("ignore", category=FutureWarning)
Run Code Online (Sandbox Code Playgroud)

  • 并将其恢复为默认行为:`warnings.filterwarnings(“ default”,category = FutureWarning)` (6认同)

Pra*_*kar 14

不要让它复杂,只需使用这两行

import warnings
warnings.filterwarnings('ignore')
Run Code Online (Sandbox Code Playgroud)


use*_*167 9

如果您知道通常会遇到什么无用的警告,则可以按消息过滤它们。

import warnings

#ignore by message
warnings.filterwarnings("ignore", message="divide by zero encountered in divide")

#part of the message is also okay
warnings.filterwarnings("ignore", message="divide by zero encountered") 
warnings.filterwarnings("ignore", message="invalid value encountered")
Run Code Online (Sandbox Code Playgroud)


pol*_*zul 9

当所有其他方法都失败时,请使用:https : //github.com/polvoazul/shutup

pip install shutup

然后添加到代码的顶部:

import shutup; shutup.please()
Run Code Online (Sandbox Code Playgroud)

免责声明:我是该存储库的所有者。我在第 5 次需要它之后写了它,但找不到任何简单的东西。


jor*_*mit 7

我意识到这仅适用于一小部分情况,但在numpy我真的很喜欢使用的上下文中np.errstate

np.sqrt(-1)
Run Code Online (Sandbox Code Playgroud)
__main__:1: RuntimeWarning: invalid value encountered in sqrt
nan
Run Code Online (Sandbox Code Playgroud)

但是,使用np.errstate

__main__:1: RuntimeWarning: invalid value encountered in sqrt
nan
Run Code Online (Sandbox Code Playgroud)
nan
Run Code Online (Sandbox Code Playgroud)

最好的部分是您只能将其应用于非常特定的代码行。


Art*_*ers 6

import sys
if not sys.warnoptions:
    import warnings
    warnings.simplefilter("ignore")
Run Code Online (Sandbox Code Playgroud)

在处理文件或添加新功能以重新启用警告时,将忽略更改为默认值。


Saf*_* CK 5


更Pythonic的方式来忽略警告


由于“ warning.filterwarnings() ”并没有抑制所有警告,我建议您使用以下方法:

import logging
    
for name in logging.Logger.manager.loggerDict.keys():
    logging.getLogger(name).setLevel(logging.CRITICAL)

#rest of the code starts here...
Run Code Online (Sandbox Code Playgroud)

或者,

如果您只想抑制一组特定的警告,那么您可以像这样进行过滤:

import logging
    
for name in logging.Logger.manager.loggerDict.keys():
    if ('boto' in name) or ('urllib3' in name) or ('s3transfer' in name) or ('boto3' in name) or ('botocore' in name) or ('nose' in name):
            logging.getLogger(name).setLevel(logging.CRITICAL)

#rest of the code starts here...
Run Code Online (Sandbox Code Playgroud)