How to disable Python warnings

A warning in Python is used to display a message which is not considered an error or exception. For instance, if we use any deprecated features in Python that are basically not allowed in the latest versions of Python, then a warning message will be displayed. Sometimes, when we are using code that we know will raise a warning, for example, a deprecated module, but we do not want to see that warning message then we can have different approaches to ignore it. Thus, in this tutorial, we will learn how to disable Warnings in Python.

Example Of Warning In Python

A warning is different from an error. In Python, errors would simply terminate the entire program and would not let it continue to run normally. Whereas, warnings would simply display a message to the users and will allow the program to continue running, irrespective of the type of warning we receive as an output. To create a warning message in Python we use warn function from the warnings module. The below-mentioned code uses the warn() function to display a warning to the user.

import warnings
  
print("Hello World")
  
# displaying the warning message 
warnings.warn('Warning Message: This is a warning message')

Output:

Hello World
<string>:6: UserWarning: Warning Message: This is a warning message

Disable all Warnings

To disable warnings in Python, we can make use of the filterwarnings() method from the warnings module in Python. The warning filter contains a list of filter parameters. If any warning message matches with that of the filter associated with that parameter then, the corresponding actions are taken upon that warning message. In our case, we will use the ignore filter parameter which will simply ignore all the warnings that are generated by that Python code.

import warnings
  
print("Hello World")
warnings.filterwarnings('ignore')
# displaying the warning message 
warnings.warn('Warning Message: This is a warning message')

Output:

Hello World

Disable Specific Warnings in Python

Suppose we want to disable specific and not all warnings in Python then we can add an additional parameter to the filterwarnings() function. For instance, in the below-mentioned code, we will disable those warnings whose text matches with ‘ not allowed’. We will display warnings that do not match the specified text.

import warnings

warnings.filterwarnings('ignore', message='not allowed')
print('hello world')
warnings.warn("not allowed")
warnings.warn("Warning: This is a warning message")

Output:

hello world
<string>:6: UserWarning: Warning: This is a warning message

Disable Warnings of Specific Category

import numpy as np
print(1/(1+np.exp(1203)))

Output:

<string>:3: RuntimeWarning: overflow encountered in exp
0.0

Upon running the following code we can see that it gives a runtime warning as we are trying to divide 1 by a huge number in exponential. Thus to ignore this type of warning we can make use of simplefilter() function in the warnings module with the category as RuntimeWarning.

import numpy as np
import warnings
warnings.simplefilter(action='ignore', category=RuntimeWarning)
print(1/(1+np.exp(1203)))

Output:

0.0

Thus in this tutorial, we have learned how to disable warnings in Python. To learn more about exceptions in Python click on the following link: Standard Exceptions In Python

Leave a Reply

Your email address will not be published.