如何忽略Python中的弃用警告

Moh*_*med 151 python warnings ignore deprecated

我一直这样:

DeprecationWarning: integer argument expected, got float
Run Code Online (Sandbox Code Playgroud)

如何让这条消息消失?有没有办法避免Python中的警告?

Edd*_*onk 183

我有这些:

/home/eddyp/virtualenv/lib/python2.6/site-packages/Twisted-8.2.0-py2.6-linux-x86_64.egg/twisted/persisted/sob.py:12:
DeprecationWarning: the md5 module is deprecated; use hashlib instead import os, md5, sys

/home/eddyp/virtualenv/lib/python2.6/site-packages/Twisted-8.2.0-py2.6-linux-x86_64.egg/twisted/python/filepath.py:12:
DeprecationWarning: the sha module is deprecated; use the hashlib module instead import sha
Run Code Online (Sandbox Code Playgroud)

修复它:

import warnings

with warnings.catch_warnings():
    warnings.filterwarnings("ignore",category=DeprecationWarning)
    import md5, sha

yourcode()
Run Code Online (Sandbox Code Playgroud)

现在你仍然得到所有其他DeprecationWarning的,但不是由以下原因造成的:

import md5, sha
Run Code Online (Sandbox Code Playgroud)

  • 真棒,非常感谢!! (花了我一点时间才意识到我还可以在这里包装非导入的代码,因为一些软件包在导入后使用时也会生成DeprecationWarnings.)非常好的方法只能沉默我已经查看过的特定DeprecationWarnings并决定我想忽略. (2认同)
  • 适用于 WIndows 和 Linux,并且完全独立于为可执行文件提供的任何参数。所以一直有效。此外,它仅禁用显式库的弃用警告,因此当下一个库被弃用时,您仍然会收到更新源代码的通知。 (2认同)

ism*_*ail 179

你应该修改你的代码,以防万一,

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

  • 这对我来说根本不起作用,仍然看到弃用警告. (19认同)
  • @ user1244215我可能错了,但我认为在你的代码中运行`warnings.filterwarnings("ignore",category = DeprecationWarning)`的地方很重要.我认为你必须在导入正在吐出警告的库之后运行它,尽管我可能会弄错. (7认同)
  • 就我而言,导致警告的代码是“from xgboost import XGBClassifier”。我必须在导入之前立即放置 `warnings.filterwarnings("ignore", category=DeprecationWarning)` 才能使其工作。 (4认同)
  • 使用iPython为我工作 (2认同)

Ste*_*202 110

warnings模块的文档:

 #!/usr/bin/env python -W ignore::DeprecationWarning
Run Code Online (Sandbox Code Playgroud)

如果你在Windows上:-W ignore::DeprecationWarning作为参数传递给Python.尽管通过强制转换为int来解决问题更好.

(请注意,在Python 3.2中,默认情况下会忽略弃用警告.)

  • 你可以设置env变量PYTHONWARNINGS这对我有用`export PYTHONWARNINGS ="ignore :: DeprecationWarning:simplejson"`来禁用来自sorl的django json deprication警告 (13认同)
  • 我希望我能做到这一点......我得到一个`/ usr/bin/env:python -W ignore :: DeprecationWarning:没有这样的文件或目录`错误.如果我在命令行上使用`-W ignore :: DeprecationWarning`选项运行python,但是/ usr/bin/env不处理它. (8认同)
  • 似乎是一个仅限Windows的解决方案. (2认同)

Tri*_*ick 26

我发现最简洁的方法(特别是在Windows上)是通过在C:\ Python26\Lib\site-packages\sitecustomize.py中添加以下内容:

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

请注意,我必须创建此文件.当然,如果您的路径不同,请将路径更改为python.


mak*_*kis 20

这些答案都不适合我,所以我会发布解决方法.我使用以下at the beginning of my main.py脚本,它工作正常.


按原样使用以下内容(复制粘贴):

import "blabla"
import "blabla"

def warn(*args, **kwargs):
    pass
import warnings
warnings.warn = warn

# more code here...
# more code here...
Run Code Online (Sandbox Code Playgroud)

  • 当所有其他解决方案都不起作用时,这种方法就起作用了。谢谢! (4认同)
  • 对于那些想要一个简单的答案的人:`warnings.warn = lambda *args, **kwargs: None` (3认同)
  • 在 3.7.3 中不适用于 AstroPy 弃用警告。:( (2认同)
  • 相同的。其他解决方案均无效。这是唯一有效的方法。 (2认同)

Tri*_*ath 8

当您只想忽略函数中的警告时,可以执行以下操作。

import warnings
from functools import wraps


def ignore_warnings(f):
    @wraps(f)
    def inner(*args, **kwargs):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("ignore")
            response = f(*args, **kwargs)
        return response
    return inner

@ignore_warnings
def foo(arg1, arg2):
    ...
    write your code here without warnings
    ...

@ignore_warnings
def foo2(arg1, arg2, arg3):
    ...
    write your code here without warnings
    ...
Run Code Online (Sandbox Code Playgroud)

只需在要忽略所有警告的函数上添加 @ignore_warnings 装饰器


rul*_*web 8

如果您使用日志记录 ( https://docs.python.org/3/library/logging.html ) 来格式化或重定向 ERROR、NOTICE 和 DEBUG 消息,则可以将 WARNINGS 从警告系统重定向到日志记录系统:

logging.captureWarnings(True)
Run Code Online (Sandbox Code Playgroud)

它将捕获带有标签“py.warnings”的警告。另外,如果您想丢弃这些警告而不记录日志,则可以使用以下命令将日志记录级别设置为 ERROR:

logging.getLogger("py.warnings").setLevel(logging.ERROR)
Run Code Online (Sandbox Code Playgroud)

它将导致所有这些警告被忽略,而不会显示在您的终端或其他任何地方。

请参阅https://docs.python.org/3/library/warnings.htmlhttps://docs.python.org/3/library/logging.html#logging.captureWarnings并将captureWarnings 设置为 True 不会捕获警告

就我而言,我使用日志系统格式化所有异常,但警告(例如 scikit-learn)不受影响。


Mar*_*les 7

码头工人解决方案

  • 在运行 python 应用程序之前禁用所有警告
    • 您也可以禁用 dockerized 测试
ENV PYTHONWARNINGS="ignore::DeprecationWarning"
Run Code Online (Sandbox Code Playgroud)


Dip*_*jar 6

蟒蛇 3

在编写代码之前,只需编写以下易于记忆的行:

import warnings

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


shy*_*ent 5

传递正确的论点?:P

更严重的是,您可以在命令行上将参数-Wi :: DeprecationWarning传递给解释器以忽略弃用警告.