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

运行一个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报警告性错误啦


上一篇:Python collections.defaultdict() 与 dict的使用和区别
下一篇:如何在vim中使用tab进行python代码补全

PythonTab微信公众号:

Python技术交流互助群 ( 请勿加多个群 ):

群1: 87464755

群2: 333646237

群3: 318130924

群4: 385100854