How to Suppress Warning Messages in Python

Problem Formulation and Solution Overview

This article will show you how to suppress warning messages in Python scripts.

Warning messages in Python are commonly used to notify the coder of a potential issue with their script. Warnings can inform the coder about any outdated or obsolete elements. An example would be if a plugin or module is no longer in use (obsolete).

A good reason to suppress these warnings would be to keep a script viable until the appropriate adjustments can be made.


πŸ’¬ Question: How would we write code to suppress these warnings?

We can accomplish this task by one of the following options:


Method 1: Suppress Warnings with filterwarnings()

This example uses Python’s built-in warnings library and calls the warn() function. This function generates and displays a warning message if the code encounters an issue. However, this does not terminate the script.

import warnings

warnings.filterwarnings('ignore')
print('The script still runs.')

To suppress any warnings in the script, import the warnings library, and pass the filterwarnings() function the argument ignore. This allows the script to continue by seamlessly suppressing all warnings.

The output of this script is sent to the terminal.

The script still runs.

Rather than using ignore, other options are available for your Python script.

ArgumentDescription
defaultPassing this argument will output the first occurrence of a matching warning. This statement will include the module name and the line where the warning occurred.
errorThis argument will convert any warning message into an error message if one is generated. Good for debugging purposes.
ignoreThis argument ignores any matching warning messages altogether. Not recommended as these warnings will need to be dealt with at some future point. Always aim for clean code!
alwaysThis argument outputs any matching warning messages. Also suitable for debugging purposes.
moduleThis argument displays a warning message only once. This is independent of the number of times it occurs.
onceThis argument displays a warning message once for the first occurrence regardless of where this occurs.

πŸ’‘ Note: Another option is to log warnings that occur in the script by using logging.warn(). This will aid in code clean-up.


Method 2: Suppress Specific Warnings with filterwarnings()

This example shows how to suppress a specific warning message based on the selected category.

import warnings

warnings.filterwarnings(action='default', category=UnicodeWarning) 

The filterwarnings() function allows the option of passing an additional argument, a category (category=UnicodeWarning). Any time a warning is encountered in the Python script, it is ignored.

The available categories are:

WarningPendingDeprecationWarning
UserWarningImportWarning
DeprecationWarningUnicodeWarning
SyntaxWarningBytesWarning
RuntimeWarningResourceWarning
FutureWarning

πŸ’‘ Note: We recommend reading up on the various types of warnings.


Method 3: Suppress Warnings with catch_warnings()

The example uses the catch_warnings() function to cover situations where a coder is aware of script issues but doesn’t necessarily want to see the warning messages.

import warnings

def suppress_warnings():
    warnings.warn('Plugin Deprecated!', DeprecationWarning)

with warnings.catch_warnings():
    warnings.simplefilter('ignore')
    suppress_warnings()

This code suppresses the warnings message for any deprecated module that will be removed from Python in a future release.

This allows the coder to work through the replacement issues of the soon-to-be outdated module while maintaining the code’s operability.


Method 4: Suppress Warnings with shutup()

This example uses shutup() to suppress any and all warnings!

For this code to run error-free, the shutup() plugin needs to be installed.

Navigate to an IDE and run the following code from the terminal.

pip install shutup!

Next, place the following line of code at the top of any Python script.

That’s it!

import shutup; shutup.please()

Method 5: Suppress Warnings with -W

This example shows how to suppress script warnings using the -W switch at the terminal. From the terminal command prompt, enter python -W ignore yourfilename.py. Press the <Enter> key to execute.

$ python -W ignore hello.py

The script will run without displaying any warning messages.


Summary

These five (5) methods of suppressing warning messages should give you enough information to select the best one for your coding requirements.

Good Luck & Happy Coding!


Programming Humor – Python

“I wrote 20 short programs in Python yesterday. It was wonderful. Perl, I’m leaving you.”xkcd