我不断收到这个:
DeprecationWarning: integer argument expected, got float
如何让这条消息消失?有没有办法避免Python中的警告?
你应该修复你的代码,但以防万一,
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
我有这些:
/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()
现在你仍然得到所有其他
DeprecationWarning
,但不是由以下原因引起的:
import md5, sha
这些答案都不适合我,所以我将发布解决此问题的方法。我使用以下
at the beginning of my main.py
脚本,效果很好。
按原样使用以下内容(复制粘贴):
def warn(*args, **kwargs):
pass
import warnings
warnings.warn = warn
或 1 行,感谢 Philippe Remy 的评论
import warnings ; warnings.warn = lambda *args,**kwargs: None
示例:
def warn(*args, **kwargs):
pass
import warnings
warnings.warn = warn
import "blabla"
import "blabla"
# more code here...
# more code here...
我发现最干净的方法(尤其是在 Windows 上)是将以下内容添加到 C:\Python26\Lib\site-packages\sitecustomize.py:
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
请注意,我必须创建此文件。当然,如果你的路径不同,请更改为python的路径。
ENV PYTHONWARNINGS="ignore::DeprecationWarning"
当您只想忽略函数中的警告时,您可以执行以下操作。
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 3
在编写代码之前,只需写下以下易于记住的行:
import warnings
warnings.filterwarnings("ignore")
传递正确的参数? :P
更严重的是,您可以在命令行上将参数 -Wi::DeprecationWarning 传递给解释器以忽略弃用警告。
将参数转换为 int。就这么简单
int(argument)
对于 python 3,只需编写以下代码即可忽略所有警告。
from warnings import filterwarnings
filterwarnings("ignore")
如果您使用的是 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")
要添加到以前的解决方案中,如果您继续在某些并行应用程序中收到警告(例如使用
multiprocessing
的应用程序,则警告过滤器也可能需要传递给子进程。您可以使用某些方法来做到这一点像这样:
import sys, os
if not sys.warnoptions:
import warnings
warnings.simplefilter("ignore")
os.environ["PYTHONWARNINGS"] = "ignore"
添加
os.environ
行将导致子进程也忽略警告。
如果您知道自己在做什么,另一种方法就是找到警告您的文件(该文件的路径显示在警告信息中),注释生成警告的行。
有点粗糙,但在上述方法不起作用后,它对我有用。
./myscrypt.py 2>/dev/null
并不是为了打击你,而是警告你,当你下次升级 python 时,你正在做的事情可能会停止工作。转换为 int 并完成。
顺便说一句。您还可以编写自己的警告处理程序。只需分配一个不执行任何操作的函数即可。 如何将 python 警告重定向到自定义流?
注释掉以下文件中的警告行:
lib64/python2.7/site-packages/cryptography/__init__.py