How to stop/disable Python warnings?

In Python programming language or in general programming, we often come across with different kinds of warnings and errors. Some are expected and some are unexpected. Developers come across the situations where he/she needs to remove the expected warning. So, In this Python tutorial article we will learn about some methods using which we can stop/disable Python warnings.

Table Of Contents

Warnings vs Error

When an error occurs in Python Programming language, the program immediately stops but not the same in the case of warnings, Python interpreter warns about certain exception and continues the program.

  • Categories of Warnings :
  • Warning
  • UserWarning
  • DeprecationWarning
  • SyntaxWarning
  • RuntimeWarning
  • FutureWarning
  • PendingDeprecationWarning
  • ImportWarning
  • UnicodeWarning
  • BytesWarning
  • ResourceWarning

warn() function

In Python Programming language we have a warn() function of warning module, which is similar to the PyErr_WarnEx() which is used in C++. This function is used to show warning message or it can also be used to ignore certain warnings which developer is expecting in the code.

Here is an example code with Warnings

CODE :

#importing the warning module
import warnings

# Creating an example warning message using warn() function.
warnings.warn('DemoWarning : This is an example warning message.')

print('abcd')

OUTPUT :

DemoWarning : This is an example warning message.
 warnings.warn('DemoWarning : This is an example warning message.')
abcd

In the above example code and output you can see, a warning message has been created using the warn() function of warning module. Now Let’s learn about warnings filter then we will learn about some methods using which we can stop/disable Python warnings.

Warnings Filter

In the warnings module we have a warning filter which contorls whether the warnings will be ignored, displayed or it will be displayed as an error(exceptions). In the warnings filter we have an ordered list of filter specifications, warnings are matched against there filter specification.

Method 1 : Using simplefilter()

First method we can use to stop/disable Python warnings is by using simplefilter() function of warnings module. This module comes pre-installed with python programming language, this method filter outs all the warnings. When actions = ‘ignore’ is passed in the parameter, then this filters all the warning.

SYNTAX : warnings.simplefilter(action, category=warnings, lineno=0, append=False)

This method receives four parameters which are as follows:

  • actions
ActionsDisposition
“default”prints the warning message with line number.
“ignore”This will not print any warning.
“always”Always prints the matching warning.
“module”Prints the first warning for each module.
“once”Prints only the first matching warning.
“error”This turns the warnings message into an error.
  • category : a class which the warning category must be a subclass.
  • lineno : Integer, warning of line number to be filtered. This should, must match.
  • append : Boolean, default is false. Default value to inseert entry is front. If append is true, then the entry will be inserted at the end.

See an example code below

CODE :

#importing warning module
import warnings

# Creating an example warning message using warn() function.
warnings.warn('DemoWarning : This is an example warning message.')

# using simplefilter() to stop/disable warnings.
warnings.simplefilter(action='ignore')

print('abcd')

OUTPUT :

abcd

In the above code and output you can see warning message has been filtered by using the simplefilter() method of warnings module. You can see action=’ignore’ has been passed which will ignore/filter all the warning message.

Method 2 : Using filterWarning()

Another method we will be using is the filterwarnings() method of warnings module. This method is similar to the method above which is the simplefilter() method but it has some finer filtration methods. In this method you can filter out all the warning from a specific given module.

This method receives six parameters which are as follows:

ActionsDisposition
“default”prints the warning message with line number.
“ignore”This will not print any warning.
“always”Always prints the matching warning.
“module”Prints the first warning for each module.
“once”Prints only the first matching warning.
“error”This turns the warnings message into an error.
  • message : String, a regular expression at the beginning of the message which must macth.
  • category : a class which the warning category must be a subclass.
  • module : String, used to set a regular expression to filter warning message of any specific module.
  • lineno : Integer, warning of line number to be filtered. This should, must match.
  • append : Boolean, default is false. Default value to inseert entry is front. If append is true, then the entry will be inserted at the end.

SYNTAX : warnings.filterwarning(action,message='',category=Warning,module='', lineno=0, append=False)

Now lets see an example code.

CODE :

#importing warning module
import warnings

# Creating an example warning message using warn() function.
warnings.warn('DemoWarning : This is an example warning message.')

# using filterwarnings() to stop/disable warnings.
warnings.filterwarnings('ignore')

print('abcd')

OUTPUT :

abcd

In the above code and output, you can see we have used warnings.filterwarning() to stop/disable Python warnings which is being generated by warnings.warn() function.

SUMMARY

In this Python tutorial article of How to stop/disable Python warnings you can see we have used two methods of warnings module to filterout all the warnings that are being generated by warnings.warn() function. Both of these methods can be used to filter out all warnings that are being generated by the Python Interpreter. Also we have learned about types of warnings and Warnings Filter.

You can always use both the methods according to your use. Like if you are working with various modules and getting some warning in any specific module, then you can use the filterwarnings() method and pass the module name as an argument in this function to filter out all the warnings generated from that module.

Make sure to read/write to code to have a better understanding of this problem.

Thanks

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top