Python warnings Module
Example
Issue a simple warning message:
import warnings
warnings.warn('This is a warning message')
print('Program continues...')
Try it Yourself »
Definition and Usage
The warnings module allows you to issue warning messages to users and control how warnings are displayed, filtered, or turned into exceptions.
Use it to alert users about deprecated features, potential issues, or non-fatal problems without stopping program execution.
Note: Warnings are typically issued for situations that don't warrant raising an exception but should be brought to the user's attention.
Members
Member | Description |
---|---|
BytesWarning | Warning type shown when mixing bytes and text incorrectly. |
catch_warnings() | Context manager to temporarily control warning filters and actions. |
DeprecationWarning | Warning type for features that will be removed in future Python versions. |
EncodingWarning | Warning type for encoding-related issues (Python 3.10+). |
filterwarnings() | Add a rule to decide whether to show, ignore, or raise certain warnings. |
formatwarning() | Turn a warning into a text string for display. |
FutureWarning | Warning type for code that will behave differently in the future. |
ImportWarning | Warning type for suspicious module import operations. |
PendingDeprecationWarning | Warning type for features planned for deprecation later. |
resetwarnings() | Clear all custom warning filters back to default behavior. |
ResourceWarning | Warning type for resource usage problems (unclosed files, etc.). |
RuntimeWarning | Warning type for questionable runtime behavior. |
showwarning() | The function that actually prints warnings (you can replace it). |
simplefilter() | Add a simple filter rule without needing regular expressions. |
SyntaxWarning | Warning type for suspicious Python syntax. |
UnicodeWarning | Warning type for Unicode encoding and decoding issues. |
UserWarning | The default warning type used by warn(). |
warn() | Show a warning message to the user. |
warn_explicit() | Show a warning with complete manual control over all details. |
Warning | The base class that all warning types inherit from. |
WarningMessage | Holds details about a warning (message, category, filename, etc.). |