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