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

Python ignores warning error

高洛峰
Release: 2016-10-19 11:52:18
Original
1607 people have browsed it

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()
Copy after login

This generates warning errors

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

import warnings
warnings.filterwarnings("ignore")
Copy after login

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
Copy after login

That’s it


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template