我总是听到这个:
DeprecationWarning: integer argument expected, got float
我怎样才能让这条信息消失?在Python中有避免警告的方法吗?
我总是听到这个:
DeprecationWarning: integer argument expected, got float
我怎样才能让这条信息消失?在Python中有避免警告的方法吗?
当前回答
这些答案都不适合我,所以我将发布我的方法来解决这个问题。我在main.py脚本的开头使用了以下代码,它工作得很好。
原封不动地使用下面的代码(复制粘贴):
def warn(*args, **kwargs):
pass
import warnings
warnings.warn = warn
例子:
import "blabla"
import "blabla"
def warn(*args, **kwargs):
pass
import warnings
warnings.warn = warn
# more code here...
# more code here...
其他回答
如果你使用的是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")
传递正确的参数?: P
更严重的是,您可以在命令行中将参数-Wi::DeprecationWarning传递给解释器,以忽略弃用警告。
如果您正在使用日志记录(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 3,只需编写下面的代码来忽略所有警告。
from warnings import filterwarnings
filterwarnings("ignore")
来自警告模块的文档:
#!/usr/bin/env python -W ignore::DeprecationWarning
如果你在Windows上:传递-W ignore::DeprecationWarning作为Python的参数。不过更好的解决方法是强制转换为int类型。
(注意,在Python 3.2中,默认情况下会忽略弃用警告。)