我总是听到这个:
DeprecationWarning: integer argument expected, got float
我怎样才能让这条信息消失?在Python中有避免警告的方法吗?
我总是听到这个:
DeprecationWarning: integer argument expected, got float
我怎样才能让这条信息消失?在Python中有避免警告的方法吗?
当前回答
我发现最干净的方法(特别是在windows上)是在C中添加以下代码:\Python26\Lib\site-packages\sitecustomize.py:
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
注意,我必须创建这个文件。当然,如果您的路径不同,请将路径更改为python。
其他回答
注释掉下面文件中的警告行:
lib64/python2.7/site-packages/cryptography/__init__.py
如果你使用的是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")
将实参转换为int。这很简单
int(argument)
有点粗糙,但它为我工作后,上述方法没有。
- myscrypt。py 2 > / dev /空
当您希望仅忽略函数中的警告时,可以执行以下操作。
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装饰器