私はこれを取得し続けます:
DeprecationWarning: integer argument expected, got float
このメッセージを消すにはどうすればよいですか? Python で警告を回避する方法はありますか?
私はこれを取得し続けます:
DeprecationWarning: integer argument expected, got float
このメッセージを消すにはどうすればよいですか? Python で警告を回避する方法はありますか?
コードを修正する必要がありますが、念のため、
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
私はこれらを持っていました:
/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
s が取得されますが、次の原因によるものは取得されません。
import md5, sha
warnings
モジュールのドキュメントから:
#!/usr/bin/env python -W ignore::DeprecationWarning
Windows を使用している場合:-W ignore::DeprecationWarning
引数として Python に渡します。intにキャストして、問題を解決することをお勧めします。
(Python 3.2 では、非推奨の警告はデフォルトで無視されることに注意してください。)
これを行う最もクリーンな方法(特にWindowsで)は、C:\ Python26 \ Lib \ site-packages\sitecustomize.pyに次を追加することです。
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
このファイルを作成する必要があることに注意してください。もちろん、パスが異なる場合は、パスをpythonに変更してください。
正しい引数を渡しますか? :P
さらに深刻な点として、コマンド ラインで引数 -Wi::DeprecationWarning をインタープリターに渡して、非推奨の警告を無視することができます。
関数でのみ警告を無視したい場合は、次のことができます。
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 デコレータを追加するだけです
引数を int に変換します。それは次のように簡単です
int(argument)
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")
それについてあなたを打ち負かすわけではありませんが、あなたがしていることは、次にpythonをアップグレードするときに機能しなくなる可能性が高いと警告されています. int に変換して完了です。
ところで。独自の警告ハンドラを作成することもできます。何もしない関数を割り当てるだけです。 Python の警告をカスタム ストリームにリダイレクトする方法は?