Python解决各种由兼容性引起的Warning

大家在使用Python的时候,肯定会接触到一些第三方的库,在我们安装的时候,会产生一下情况:

情况一:

会产生第三方库的版本和Python版本不兼容而引发的Warning或Error。如果是错误的话,则需要查看你的Python和第三方库的匹配版本。

情况二:

会产生第三方库和第三方库兼容性的冲突。如果是错误的话,则需要查看双方匹配的版本。

Warning解决办法

常见的Warning有:UserWarning、DeprecationWarning、RuntimeWarning、SyntaxWarning、FutureWarning、OverflowWarning、PendingDeprecationWarning等等,如果不想让控制台提示Warning信息可用一下方法解决

import warnings

warnings.filterwarnings("ignore")

分析一下:

def filterwarnings(action, message="", category=Warning, module="", lineno=0,
                   append=False):
    """Insert an entry into the list of warnings filters (at the front).

    'action' -- one of "error", "ignore", "always", "default", "module",
                or "once"
    'message' -- a regex that the warning message must match
    'category' -- a class that the warning must be a subclass of
    'module' -- a regex that the module name must match
    'lineno' -- an integer line number, 0 matches all warnings
    'append' -- if true, append to the list of filters
    """
    assert action in ("error", "ignore", "always", "default", "module",
                      "once"), "invalid action: %r" % (action,)
    assert isinstance(message, str), "message must be a string"
    assert isinstance(category, type), "category must be a class"
    assert issubclass(category, Warning), "category must be a Warning subclass"
    assert isinstance(module, str), "module must be a string"
    assert isinstance(lineno, int) and lineno >= 0, \
           "lineno must be an int >= 0"

    if message or module:
        import re

    if message:
        message = re.compile(message, re.I)
    else:
        message = None
    if module:
        module = re.compile(module)
    else:
        module = None

    _add_filter(action, message, category, module, lineno, append=append)

可以发现,在函数使用中有如下参数:

“message”-警告消息必须匹配的正则表达式

“category”-警告必须是其子类的类

“module”-模块名称必须匹配的正则表达式

“lineno”一个整数行号,0与所有警告匹配

“append”如果为true,则追加到筛选器列表

显然,我们把action的状态改为“ignore”即可解决

希望对大家有帮助。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

戒酒的李白-Lisage

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值