如何在Python中忽略弃用警告
我一直在想:如何在Python中忽略弃用警告,python,warnings,deprecated,ignore,Python,Warnings,Deprecated,Ignore,我一直在想: DeprecationWarning: integer argument expected, got float 我怎样才能让这个信息消失?有没有办法避免Python中的警告?传递正确的参数P 更重要的是,您可以将命令行上的参数-Wi::DeprecationWarning传递给解释器,以忽略deprecation警告。将参数转换为int int(argument) 根据以下文件: 如果您在Windows上:将-W ignore::DeprecationWarning作为参数传
DeprecationWarning: integer argument expected, got float
我怎样才能让这个信息消失?有没有办法避免Python中的警告?传递正确的参数P
更重要的是,您可以将命令行上的参数-Wi::DeprecationWarning传递给解释器,以忽略deprecation警告。将参数转换为int
int(argument)
根据以下文件:
如果您在Windows上:将-W ignore::DeprecationWarning
作为参数传递给Python。更好地解决问题,通过铸造
(请注意,在Python3.2中,默认情况下会忽略弃用警告。)不想就此痛斥您,但会警告您,下次升级Python时,您所做的可能会停止工作。转换为int并完成它 顺便说一句,您也可以编写自己的警告处理程序。只分配一个不做任何事情的函数。
您应该修复代码,但以防万一
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()
现在,您仍然可以看到所有其他的弃用警告
s,但不是由以下原因引起的警告:
import md5, sha
我发现最干净的方法(尤其是在windows上)是将以下内容添加到C:\Python26\Lib\site packages\sitecustomize.py:
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
注意,我必须创建这个文件。当然,如果您的路径不同,请更改python路径。如果您只想忽略函数中的警告,可以执行以下操作
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\u warnings decorator即可这些答案都不适用于我,因此我将发布我的解决方法。我在main.py脚本的开头使用了以下
,效果很好。
按原样使用以下内容(复制粘贴): 例如:
import "blabla"
import "blabla"
def warn(*args, **kwargs):
pass
import warnings
warnings.warn = warn
# more code here...
# more code here...
Docker解决方案
- 在运行python应用程序之前禁用所有警告
- 您也可以禁用停靠的测试
如果您知道自己在做什么,另一种方法是找到警告您的文件(文件路径显示在警告信息中),对生成警告的行进行注释。 Python 3 在编写代码之前,只需写下容易记住的行:
import warnings
warnings.filterwarnings("ignore")
对于Python3,只需编写以下代码即可忽略所有警告
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")
或者试试这个
import warnings
def fxn():
warnings.warn("deprecated", DeprecationWarning)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
fxn()
import warnings
warnings.filterwarnings("ignore")
如果使用日志()格式化或重定向错误、通知和调试消息,则可以将警告从警告系统重定向到日志系统:
logging.captureWarnings(True)
看到和
在我的情况下,我正在使用日志系统格式化所有异常,但警告(例如scikit学习)没有受到影响。注释掉下面文件中的警告行:
lib64/python2.7/site-packages/cryptography/__init__.py
我希望我能让这一切顺利。。。我得到一个
/usr/bin/env:python-W ignore::DeprecationWarning:没有这样的文件或目录
错误。如果我使用命令行上的-W ignore::DeprecationWarning
选项运行python,它会起作用,但是/usr/bin/env不会处理它。这似乎是一个仅限windows的解决方案。您可以设置env变量pythonwarning,这对我很有效导出pythonwarning=“ignore::DeprecationWarning:simplejson”
从中禁用django json去润滑警告sorl@yvess如果这是一个答案,我会投赞成票。似乎是一种忽略系统范围内特定警告的干净方法。我把它放在我的~/.profile中。很好。大家好,我们可以了解一下如何将此弃用警告消息转换为信息类型的消息。我只想在控制台上显示消息,而不是将其归类为任何类型的警告。太棒了,非常感谢!!(我花了一点时间才意识到我也可以在这里包装非导入代码,因为有些包在导入后使用时也会生成弃用警告。)这是一种很好的方法,可以只沉默我已经看过并决定忽略的特定弃用警告。使用iPythons对我有效这对我根本不起作用,仍然看到弃用警告。@user1244215我可能错了,但我认为在代码中运行warnings.filterwarnings(“忽略”,category=deprecationwarnings)
。我认为您必须在导入发出警告的库后运行此命令,尽管我可能弄错了。@需要编写YourLife类别,以便您仍然可以看到其他类型的警告,如RuntimeWarning等。在我的例子中,导致警告的代码是来自xgboost import XGBClassifier的。我必须在导入之前立即添加警告。过滤器警告(“忽略”,category=DeprecationWarning)
,这样才能使其生效。只有当它确实是他自己的代码,而不是来自某个第三方包时,该建议才起作用。当所有其他解决方案都不起作用时,该建议才起作用。谢谢这也救了我。很高兴我能提供帮助。在3.7.3中不适用于AstroPy弃用警告:(如果警告来自第三方库怎么办?
lib64/python2.7/site-packages/cryptography/__init__.py