我总是听到这个:
DeprecationWarning: integer argument expected, got float
我怎样才能让这条信息消失?在Python中有避免警告的方法吗?
我总是听到这个:
DeprecationWarning: integer argument expected, got float
我怎样才能让这条信息消失?在Python中有避免警告的方法吗?
当前回答
来自警告模块的文档:
#!/usr/bin/env python -W ignore::DeprecationWarning
如果你在Windows上:传递-W ignore::DeprecationWarning作为Python的参数。不过更好的解决方法是强制转换为int类型。
(注意,在Python 3.2中,默认情况下会忽略弃用警告。)
其他回答
并不是要痛打你,而是警告你,当你下次升级python时,你正在做的事情可能会停止工作。转换为int并完成它。
顺便说一句。您还可以编写自己的警告处理程序。只要分配一个什么都不做的函数。 如何重定向python警告自定义流?
有点粗糙,但它为我工作后,上述方法没有。
- myscrypt。py 2 > / dev /空
如果你使用的是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")
这些答案都不适合我,所以我将发布我的方法来解决这个问题。我在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...
我发现最干净的方法(特别是在windows上)是在C中添加以下代码:\Python26\Lib\site-packages\sitecustomize.py:
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
注意,我必须创建这个文件。当然,如果您的路径不同,请将路径更改为python。