219

私はこれを取得し続けます:

DeprecationWarning: integer argument expected, got float

このメッセージを消すにはどうすればよいですか? Python で警告を回避する方法はありますか?

4

16 に答える 16

260

コードを修正する必要がありますが、念のため、

import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning) 
于 2009-05-18T19:01:48.350 に答える
213

私はこれらを持っていました:

/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()

これで、他のすべてのDeprecationWarnings が取得されますが、次の原因によるものは取得されません。

import md5, sha
于 2009-10-28T23:24:01.537 に答える
136

warningsモジュールのドキュメントから:

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

Windows を使用している場合:-W ignore::DeprecationWarning引数として Python に渡します。intにキャストして、問題を解決することをお勧めします。

(Python 3.2 では、非推奨の警告はデフォルトで無視されることに注意してください。)

于 2009-05-18T18:50:07.827 に答える
32

これを行う最もクリーンな方法(特にWindowsで)は、C:\ Python26 \ Lib \ site-packages\sitecustomize.pyに次を追加することです。

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

このファイルを作成する必要があることに注意してください。もちろん、パスが異なる場合は、パスをpythonに変更してください。

于 2010-03-04T17:35:36.753 に答える
5

正しい引数を渡しますか? :P

さらに深刻な点として、コマンド ラインで引数 -Wi::DeprecationWarning をインタープリターに渡して、非推奨の警告を無視することができます。

于 2009-05-18T18:49:35.567 に答える
4

関数でのみ警告を無視したい場合は、次のことができます。

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 デコレータを追加するだけです

于 2017-12-04T21:02:39.940 に答える
3

引数を int に変換します。それは次のように簡単です

int(argument)
于 2009-05-18T18:49:38.453 に答える
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")
于 2020-03-08T04:53:54.833 に答える
-2

それについてあなたを打ち負かすわけではありませんが、あなたがしていることは、次にpythonをアップグレードするときに機能しなくなる可能性が高いと警告されています. int に変換して完了です。

ところで。独自の警告ハンドラを作成することもできます。何もしない関数を割り当てるだけです。 Python の警告をカスタム ストリームにリダイレクトする方法は?

于 2009-05-18T18:55:05.603 に答える