简体   繁体   English

如何忽略 Python 中的弃用警告

[英]How to ignore deprecation warnings in Python

I keep getting this :我不断得到这个:

DeprecationWarning: integer argument expected, got float

How do I make this message go away?如何让这条消息消失? Is there a way to avoid warnings in Python?有没有办法避免 Python 中的警告?

You should just fix your code but just in case,您应该修复您的代码,但以防万一,

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

I had these:我有这些:

/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

Fixed it with:修复它:

import warnings

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

yourcode()

Now you still get all the other DeprecationWarning s, but not the ones caused by:现在你仍然得到所有其他的DeprecationWarning s,但不是由以下原因引起的:

import md5, sha

From documentation of the warnings module :warnings模块的文档:

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

If you're on Windows: pass -W ignore::DeprecationWarning as an argument to Python.如果您使用的是 Windows:将-W ignore::DeprecationWarning作为参数传递给 Python。 Better though to resolve the issue, by casting to int .最好通过转换为int来解决问题。

(Note that in Python 3.2, deprecation warnings are ignored by default.) (请注意,在 Python 3.2 中,默认情况下会忽略弃用警告。)

None of these answers worked for me so I will post my way to solve this.这些答案都不适合我,所以我会发布我的方法来解决这个问题。 I use the following at the beginning of my main.py script and it works fine.at the beginning of my main.py脚本at the beginning of my main.py使用了以下内容at the beginning of my main.py它工作正常。


Use the following as it is (copy-paste it):按原样使用以下内容(复制粘贴):

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

Example:例子:

import "blabla"
import "blabla"

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

# more code here...
# more code here...

I found the cleanest way to do this (especially on windows) is by adding the following to C:\\Python26\\Lib\\site-packages\\sitecustomize.py:我发现最干净的方法(尤其是在 Windows 上)是将以下内容添加到 C:\\Python26\\Lib\\site-packages\\sitecustomize.py:

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

Note that I had to create this file.请注意,我必须创建此文件。 Of course, change the path to python if yours is different.当然,如果您的路径不同,请将路径更改为python。

Docker Solution码头工人解决方案

  • Disable ALL warnings before running the python application在运行 python 应用程序之前禁用所有警告
    • You can disable your dockerized tests as well您也可以禁用 dockerized 测试
ENV PYTHONWARNINGS="ignore::DeprecationWarning"

Python 3蟒蛇 3

Just write below lines that are easy to remember before writing your code:在编写代码之前,只需编写以下易于记忆的行:

import warnings

warnings.filterwarnings("ignore")

Pass the correct arguments?传递正确的论点? :P :P

On the more serious note, you can pass the argument -Wi::DeprecationWarning on the command line to the interpreter to ignore the deprecation warnings.更严重的是,您可以将命令行上的参数 -Wi::DeprecationWarning 传递给解释器以忽略弃用警告。

When you want to ignore warnings only in functions you can do the following.当您只想忽略函数中的警告时,您可以执行以下操作。

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

Just add the @ignore_warnings decorator on the function you want to ignore all warnings只需在要忽略所有警告的函数上添加 @ignore_warnings 装饰器

For python 3, just write below codes to ignore all warnings.对于python 3,只需编写以下代码即可忽略所有警告。

from warnings import filterwarnings
filterwarnings("ignore")

Convert the argument to int.将参数转换为 int。 It's as simple as就这么简单

int(argument)

Try the below code if you're Using Python3:如果您使用的是 Python3,请尝试以下代码:

import sys

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

or try this...或者试试这个...

import warnings

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

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

or try this...或者试试这个...

import warnings
warnings.filterwarnings("ignore")

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

If you are using logging ( https://docs.python.org/3/library/logging.html ) to format or redirect your ERROR, NOTICE, and DEBUG messages, you can redirect the WARNINGS from the warning system to the logging system:如果您使用日志记录 ( https://docs.python.org/3/library/logging.html ) 来格式化或重定向您的 ERROR、NOTICE 和 DEBUG 消息,您可以将警告从警告系统重定向到日志记录系统:

logging.captureWarnings(True)

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

In my case, I was formatting all the exceptions with the logging system, but warnings (eg scikit-learn) were not affected.就我而言,我使用日志系统格式化所有异常,但警告(例如 scikit-learn)不受影响。

Not to beat you up about it but you are being warned that what you are doing will likely stop working when you next upgrade python.不是为了打击你,而是警告你,当你下次升级 python 时,你正在做的事情可能会停止工作。 Convert to int and be done with it.转换为 int 并完成它。

BTW.顺便提一句。 You can also write your own warnings handler.您还可以编写自己的警告处理程序。 Just assign a function that does nothing.只需分配一个什么都不做的函数。 How to redirect python warnings to a custom stream? 如何将 python 警告重定向到自定义流?

注释掉以下文件中的警告行

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM