Ignore Warnings in Python
Warning alerts the developer of unexpected conditions. It is not as critical as errors in python. The program still runs if we get warnings it doesn’t halt the execution of the program.
There are numerous warnings issued for a variety of reasons, including
- Syntax-related Warning, raised due to user code
- Alert regarding a feature that is anticipated to become obsolete in the future.
- Technical inaccuracies when importing modules
By the way, indicators indicate something may be wrong and needs to be fixed.
Considering you are already familiar with the warn function found in the warning modules that Python programs utilize to display warnings.
Let's get to the subject of this post, which is how to ignore warnings in python.Depending on our use cases, there are a variety of circumstances where we may disable warnings. Let's examine each one in detail.
1. How to use the filterwarnings warnings function to disable Python warnings?
Assuming you are already aware, we can utilize the warn method to alert users when creating business logic about particular scenarios.
Here, we're looking at how to filter particular warnings. For this, we can make use of the filterwarnings method.
Using filterwarnings, we can primarily address two usage cases.
Dismiss all warnings
If necessary, use
filterwarnings('ignore') to suppress all warnings generated by program code. Here is the demonstration's program code.
import warnings warnings.filterwarnings('ignore') warnings.warn('ALERT!') warnings.warn('CAUTION!') print("Hi there")
Result: Hi there
Ignore some specific warnings only
In such a case we can use filterwarnings function with action ignore with appropriate regular expression string that matches with a warning message. Let's understand with the below example
Result: UserWarning: ALERT! warnings.warn('ALERT!') Hi there
Explanation: To match any warnings that contain the term "CAUTION," we have created matching rules as ".*CAUTION.*" in this instance.
Note: No matter if the string contains lowercase, uppercase, or mixed case letters, regular expressions are case-insensitive.
2. How can I turn off the warning using the Warning control?
The warning control command
-Wignore allows us to suppress warnings while running any Python file. Observe the example below:
Say we have the following py script containing the following lines of code
import warnings warnings.warn('ALERT!') warnings.warn('CAUTION, It can prompt warnings!') print("Hi there")
Once this file has been executed like:
Result: UserWarning: ALERT! warnings.warn('ALERT!') UserWarning: CAUTION, It can prompt warnings! warnings.warn('CAUTION, It can prompt warnings!') Hi there
However, we may run the same file as follows if we want to run it without producing a warning message:
python3 -Wignore base.py
Result: Hi there
3. When should I use the simplefilter function to suppress warnings?
You can dismiss warnings by using the simplefilter function too. In contrast to filterwarnings, which can be used to filter warnings of a specific module, simplefilter is used to ignore alerts for all modules.