我总是听到这个:
DeprecationWarning: integer argument expected, got float
我怎样才能让这条信息消失?在Python中有避免警告的方法吗?
我总是听到这个:
DeprecationWarning: integer argument expected, got float
我怎样才能让这条信息消失?在Python中有避免警告的方法吗?
当前回答
有点粗糙,但它为我工作后,上述方法没有。
- 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装饰器
如果您正在使用日志记录(https://docs.python.org/3/library/logging.html)来格式化或重定向您的ERROR、NOTICE和DEBUG消息,您可以将WARNINGS从警告系统重定向到日志系统:
logging.captureWarnings(True)
它将捕获带有“py.warnings”标签的警告。此外,如果你想在不记录日志的情况下丢弃这些警告,你可以使用以下命令将日志级别设置为ERROR:
logging.getLogger("py.warnings").setLevel(logging.ERROR)
它将导致所有这些警告被忽略,而不会显示在您的终端或其他任何地方。
参见https://docs.python.org/3/library/warnings.html和https://docs.python.org/3/library/logging.html#logging.captureWarnings, captureWarnings设置为True不会捕获警告
在我的例子中,我用日志系统格式化了所有的异常,但是警告(例如scikit-learn)没有受到影响。
并不是要痛打你,而是警告你,当你下次升级python时,你正在做的事情可能会停止工作。转换为int并完成它。
顺便说一句。您还可以编写自己的警告处理程序。只要分配一个什么都不做的函数。 如何重定向python警告自定义流?
我有这些:
/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
有点粗糙,但它为我工作后,上述方法没有。
- myscrypt。py 2 > / dev /空