我总是听到这个:
DeprecationWarning: integer argument expected, got float
我怎样才能让这条信息消失?在Python中有避免警告的方法吗?
我总是听到这个:
DeprecationWarning: integer argument expected, got float
我怎样才能让这条信息消失?在Python中有避免警告的方法吗?
当前回答
如果你使用的是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")
其他回答
注释掉下面文件中的警告行:
lib64/python2.7/site-packages/cryptography/__init__.py
对于python 3,只需编写下面的代码来忽略所有警告。
from warnings import filterwarnings
filterwarnings("ignore")
我有这些:
/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
并不是要痛打你,而是警告你,当你下次升级python时,你正在做的事情可能会停止工作。转换为int并完成它。
顺便说一句。您还可以编写自己的警告处理程序。只要分配一个什么都不做的函数。 如何重定向python警告自定义流?
Python 3
在编写代码之前,只需在下面写下容易记住的行:
import warnings
warnings.filterwarnings("ignore")