Problem Formulation and Solution Overview
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.
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.
Rather than using
ignore, other options are available for your Python script.
|default||Passing 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.|
|error||This argument will convert any warning message into an error message if one is generated. Good for debugging purposes.|
|ignore||This 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!|
|always||This argument outputs any matching warning messages. Also suitable for debugging purposes.|
|module||This argument displays a warning message only once. This is independent of the number of times it occurs.|
|once||This 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)
function allows the option of passing an additional argument, a
category=UnicodeWarning). Any time a warning is encountered in the Python script, it is ignored.
The available categories are:
💡 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.
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.
Programming Humor – Python
At university, I found my love of writing and coding. Both of which I was able to use in my career.
During the past 15 years, I have held a number of positions such as:
In-house Corporate Technical Writer for various software programs such as Navision and Microsoft CRM
Corporate Trainer (staff of 30+)
Implementation Specialist for Navision and Microsoft CRM
Senior PHP Coder