HOWTO · Python

How to Suppress Warnings in Python

Learn how to suppress all or selected Python warnings in code, limit suppression to a block, or configure warning filters from the command line and environment.

On this page

To suppress Python warnings, choose a scope: use warnings.filterwarnings() for a script, python -W ignore script.py for one process, or set PYTHONWARNINGS=ignore before launching Python. You can also filter one warning category or use a context manager to suppress messages only in a particular block.

Warnings report conditions that may need attention, such as use of a deprecated API. They do not stop execution by themselves, while exceptions represent runtime failures that may interrupt a particular operation or propagate if unhandled. Read a warning before hiding it: suppression can conceal a compatibility issue that matters later.

Suppress warnings with the warnings module

The standard-library warnings module lets a script filter messages. Call warnings.filterwarnings("ignore") to ignore warnings broadly for the process, or pass criteria such as a category or message to target a subset. Filters affect warnings that match them; other warnings remain visible. Put a narrow filter near the code that needs it, and remove it when you have addressed the underlying issue.

Run this self-contained example:

import warnings

warnings.filterwarnings("ignore", category=UserWarning, message="cache is stale")
warnings.warn("cache is stale", UserWarning)
print("program continues")
program continues

The filter matches UserWarning messages beginning with the supplied text, so the matching warning is hidden. The final print demonstrates that execution continues after the warning call.

For a temporary filter, use warnings.catch_warnings() and install the filter inside its with block. When the block exits, Python restores the warning-filter state that was active when it began. This is useful around a known noisy operation when you want unrelated code to keep its normal warning policy. For example, the pattern is with warnings.catch_warnings(): warnings.simplefilter("ignore", category=DeprecationWarning); operation(). Keep the operation inside the block; calls after it use the restored filters.

Suppress warnings from the command line

Pass Python’s -W option before the script name to set a warning filter at interpreter startup. To ignore warnings for that process, run python -W ignore script.py or the equivalent compact form python -Wignore script.py. The protected spelling -Wignore is the option immediately followed by its ignore action. Replace script.py with your file name and retain any script arguments after it.

This option applies when that interpreter starts and does not edit the script. It is useful when running a tool you cannot change or when you want a one-off invocation. A command-line filter can hide useful diagnostics for the entire run, so prefer a category-specific filter when broad suppression would obscure information you need.

Suppress warnings with the PYTHONWARNINGS environment variable

Set PYTHONWARNINGS before starting Python to supply warning-filter actions to the new process. For example, in a POSIX shell use PYTHONWARNINGS=ignore python script.py; in PowerShell use $env:PYTHONWARNINGS = "ignore" and then run python script.py. These assignments are shell-specific, and the variable must be present in the environment when the interpreter launches.

The environment setting is useful when a launcher or deployment environment controls the process. It affects Python processes started with that environment, rather than changing the warning policy of an already-running interpreter. Remove the variable or set a narrower filter when you want warnings visible again. Across all three methods, suppress only messages you have evaluated, and revisit the filter as dependencies and APIs change.