Skip to main content

Python: How to Suppress Warnings

Warnings in Python are useful messages that alert you to potential issues in your code that are not critical enough to raise an exception. For example, you might be warned about using a deprecated function or an outdated language feature. While it's usually best to address the root cause of a warning, there are times when you may need to suppress them, especially if they are noisy, outside of your control (e.g., from a third-party library), or have been reviewed and deemed harmless.

This guide will demonstrate the different ways to suppress warnings in Python, from global "catch-all" methods to the recommended best practice of targeted, temporary suppression.

Understanding How Warnings are Generated

You can generate a warning using the built-in warnings module. This is useful for understanding how suppression works.

Example of code generating a warning

import warnings

def deprecated_function():
warnings.warn(
"This function is deprecated and will be removed in a future version.",
DeprecationWarning
)
print("... old function is running ...")

deprecated_function()

Output (will appear in stderr, often highlighted in color):

main.py:4: DeprecationWarning: This function is deprecated and will be removed in a future version.
warnings.warn(
... old function is running ...

Method 1: Using the Command-Line Flag (-W ignore)

The simplest way to suppress all warnings is by using the -W ignore flag when you run your Python script from the terminal.

Solution:

python -W ignore your_script.py

Output:

... old function is running ...

In this way all warnings generated by the script will be completely hidden.

warning

This is a "blunt instrument" that hides all warnings, including potentially useful ones from other parts of your application or its dependencies. Use it with caution.

Method 2: Suppressing Warnings Globally in Your Code

You can also suppress all warnings from within your script using the warnings.simplefilter() function.

Solution:

import warnings
import sys

# This check ensures that a command-line flag (like -W) takes precedence.
if not sys.warnoptions:
warnings.simplefilter("ignore")

def deprecated_function():
warnings.warn("This is deprecated.", DeprecationWarning)
print("... old function is running ...")

deprecated_function()

Output:

... old function is running ...

This method is useful if you cannot control the command-line arguments used to run your script. However, like the -W flag, it is a global change that affects your entire program.

Method 3: Temporarily Suppressing Warnings (Best Practice)

The safest and most recommended way to suppress a warning is to do so only for the specific lines of code that you know will generate it. This is achieved using the warnings.catch_warnings() context manager.

Solution: this pattern ensures that the warning filter is changed only inside the with block, and is restored to its original state afterward.

import warnings

def deprecated_function():
warnings.warn("This is deprecated.", DeprecationWarning)
print("... old function is running ...")

# The warning will appear here
print("Calling function normally:")
deprecated_function()

print("\nCalling function with warning suppressed:")
with warnings.catch_warnings():
# Inside this block, we can change the filter.
warnings.simplefilter("ignore")
deprecated_function()

print("\nCalling function again (warning is restored):")
# The warning will reappear here because we are outside the 'with' block.
deprecated_function()

Output:

Calling function normally:
<main.py>:4: DeprecationWarning: This is deprecated.
... old function is running ...

Calling function with warning suppressed:
... old function is running ...

Calling function again (warning is restored):
<main.py>:4: DeprecationWarning: This is deprecated.
... old function is running ...
note

This is the preferred method because it is targeted and does not hide other potentially important warnings in your application.

Bonus: Suppressing a Specific Category of Warning

Instead of ignoring all warnings, it's even better to ignore only the specific category of warning you expect.

Solution:

import warnings

def deprecated_function():
warnings.warn("This is deprecated.", DeprecationWarning)
print("... old function is running ...")

with warnings.catch_warnings():
# Only ignore warnings of the category 'DeprecationWarning'
warnings.simplefilter("ignore", category=DeprecationWarning)
deprecated_function()

Output:

... old function is running ...

This is the most precise and safest method, as it will still show other types of warnings (e.g., RuntimeWarning).

Conclusion

MethodScopeBest For
-W ignoreGlobal (Entire Script)Quick, temporary suppression from the command line.
warnings.simplefilter("ignore")Global (Entire Script)Suppressing all warnings from within the code.
with warnings.catch_warnings():Local (Specific Code Block)Best practice. Targeted suppression without affecting the rest of the program.

While it's easy to silence all warnings, the most professional and safest approach is to be as specific as possible. Use the warnings.catch_warnings() context manager to temporarily suppress a specific category of warning for a known line of code.