Python Glossary Русский

all Python 3warningsterm

warnings

module

Issues warnings and controls them: show, hide, turn into errors or capture them in tests.

Syntax

import warnings

A warning reports a problem that does not stop the program, most often a deprecated API; warnings.warn() issues it, and filters decide whether it is shown. The same warning from the same place is printed only once by default, and DeprecationWarning is hidden outside __main__, so it is easy to miss. For messages about the program's operation use logging; in tests use catch_warnings(record=True) or the -W error flag.

Examples

import warnings
with warnings.catch_warnings(record=True) as caught:
    warnings.simplefilter("always")
    warnings.warn("old API")
print(caught[0].category.__name__, caught[0].message)  # → UserWarning old API

See also

Open in the full glossarydocs.python.orgReport an inaccuracy