Python ignores warning errors

created at 06-27-2021 views: 3

description

Errors are often encountered in python development, but warning usually does not affect the operation of the program, and sometimes it is particularly annoying. Let's talk about how to ignore warning errors.

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

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

This produces a warning error

solution

So how to control the output of warning errors? Very simple

import warnings
warnings.filterwarnings("ignore")

This ignores the output of warning errors. It's very simple~~

Someone wants to ask how to ignore the warning and error output on the command line? It's also very simple:

python -W ignore yourscript.py

now it's ok

Please log in to leave a comment.