Home >Backend Development >Python Tutorial >Python ignores warning error

Python ignores warning error

高洛峰
高洛峰Original
2016-10-19 11:52:181617browse

Errors are often encountered in Python development, but warnings usually do not affect the running of the program, and sometimes they are particularly annoying. Let's talk about how to ignore warning errors.

Before talking about ignoring warnings, let’s first talk about how to actively generate warning errors. The warnings module is used here. See the following code:

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

This generates warning errors

So how to control warning errors? What about the output? It's very simple

import warnings
warnings.filterwarnings("ignore")

This way the output of warning errors is ignored. It's very simple~~

Someone else wants to ask how to ignore the warning error output under the command line? It’s also very simple:

python -W ignore yourscript.py

That’s it


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn