如何忽略Python中的弃用警告

时间:2009-05-18 18:42:44

标签: python warnings deprecated ignore

我一直这样:

DeprecationWarning: integer argument expected, got float

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

16 个答案:

答案 0 :(得分:173)

我有这些:

/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

修正了:

import warnings

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

yourcode()

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

import md5, sha

答案 1 :(得分:162)

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

import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning) 

答案 2 :(得分:102)

来自warnings module的文档:

 #!/usr/bin/env python -W ignore::DeprecationWarning

如果你在Windows上:将-W ignore::DeprecationWarning作为参数传递给Python。最好通过转换为int来解决问题。

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

答案 3 :(得分:26)

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

import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)

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

答案 4 :(得分:13)

这些答案都不适合我,所以我会发布解决方法。我使用以下.tgz脚本,它可以正常工作。

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

at the beginning of my main.py

答案 5 :(得分:6)

传递正确的参数? :P

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

答案 6 :(得分:5)

将参数转换为int。它就像

一样简单
int(argument)

答案 7 :(得分:3)

如果您只想在功能中忽略警告,可以执行以下操作。

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
    ...

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

答案 8 :(得分:2)

如果您使用的是Python3,请尝试以下代码:

import sys

if not sys.warnoptions:
    import warnings
    warnings.simplefilter("ignore")

或尝试一下...

import warnings

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

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    fxn()

或尝试一下...

import warnings
warnings.filterwarnings("ignore")

答案 9 :(得分:1)

不要打扰你,但是你被警告说,当你下次升级python时,你正在做的事情可能会停止工作。转换为int并完成它。

顺便说一句。您也可以编写自己的警告处理程序。只需指定一个什么都不做的功能。 How to redirect python warnings to a custom stream?

答案 10 :(得分:1)

  

Python 3

只需在编写代码之前在容易记住的几行下面写上

import warnings

warnings.filterwarnings("ignore")

答案 11 :(得分:0)

Docker解决方案

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

答案 12 :(得分:0)

如果您知道自己在做什么,另一种方法就是找到警告您的文件(文件的路径显示在警告信息中),注释生成警告的行。 < / p>

答案 13 :(得分:0)

对于python 3,只需编写以下代码即可忽略所有警告。

gcloud projects list

答案 14 :(得分:0)

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

logging.captureWarnings(True)

请参见https://docs.python.org/3/library/warnings.htmlhttps://docs.python.org/3/library/logging.html#logging.captureWarnings

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

答案 15 :(得分:-1)

在以下文件中注释警告行。

lib64 / python2.7 / site-packages / cryptography / init .py