Suppressing Python Warnings

Posted on November 17th, 2011

Every so often you run into a situation where a piece of code will knowingly raise a warning that you might want to hide from the end user. Good example of this would be a deprecation warning.

Luckily Python provides a handy context manager that can help you catch and filter various warnings.
Here’s a quick example of how to go about doing just that:

# Import warnings module
import warnings

# Initialize context manager
with warnings.catch_warnings ():
    # Suppress warnings
    warnings.simplefilter ('ignore')

    # Put your warning prone code here

Hope this is useful!

No Comments »