python如何忽略警告(warning) 错误

python如何忽略警告(warning) 错误[python基础教程]

运行一个python文件,一直报warning的错误,很是郁闷,于是想到可以忽略这些错误。

下面来说一下如何忽略这些警告。

python -W ignore yourscript.py

import warnings
def fxn():
    warnings.warn("deprecated", DeprecationWarning)
with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    fxn()

如果不想使用以上方法,可以直接在python源代码中的开始添加如下代码:

import warnings
warnings.filterwarnings("ignore")

这样就可以忽略python报警告性错误啦

来源:PY学习网:原文地址:https://www.py.cn/article.html

hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » python如何忽略警告(warning) 错误