Python | Issue Warning Message
Last Updated :
12 Jun, 2019
Problem - To have a program that can issue warning messages (e.g., about deprecated features or usage problems).
Code #1 : Use the warnings.warn()
function
Python3 1==
import warnings
def func(x, y, logfile = None, debug = False):
if logfile is not None:
warnings.warn('logfile argument deprecated',
DeprecationWarning)
The arguments to
warn()
are a warning message along with a warning class, which is typically one of the following: UserWarning, DeprecationWarning, SyntaxWarning, RuntimeWarning, ResourceWarning, or FutureWarning.
The handling of warnings depends on how the interpreter is executed and other configuration. If Python with the
-W all
option is run, the following output is obtained:
bash % python3 -W all example.py
example.py:5: DeprecationWarning: logfile argument is deprecated
warnings.warn('logfile argument is deprecated', DeprecationWarning)
Normally, warnings just produce output messages on standard error. To turn warnings into exceptions, use the
-W error
option.
Code #2 :
bash % python3 -W error example.py
Traceback (most recent call last):
File "example.py", line 10, in
func(2, 3, logfile ='log.txt')
File "example.py", line 5, in func
warnings.warn('logfile argument is deprecated',
DeprecationWarning)
DeprecationWarning: logfile argument is deprecated
bash %
Issuing a warning message is often a useful technique for maintaining software and assisting users with issues that don’t necessarily rise to the level of being a full-fledged exception.
Code #3 : Warning message generated by destroying a file without closing it.
Python3 1==
import warnings
warnings.simplefilter('always')
f = open('/etc/passwd')
del f
Output :
__main__:1: ResourceWarning: unclosed file
<_io.TextIOWrapper name='/etc/passwd' mode='r' encoding='UTF-8'>
- By default, not all warning messages appear. The -W option to Python can control the output of warning messages.
- -W all will output all warning messages, -W ignore ignores all warnings, and -W error turns warnings into exceptions.
- As an alternative, one can use the warnings.simplefilter() function to control output. An argument of always makes all warning messages appear, ignore ignores all warnings, and error turns warnings into exceptions.
- The warnings module provides a variety of more advanced configuration options related to the filtering and handling of warning messages.
Similar Reads
Warnings in Python Warnings are provided to warn the developer of situations that aren't necessarily exceptions. Usually, a warning occurs when there is some obsolete of certain programming elements, such as keyword, function or class, etc. A warning in a program is distinct from an error. Python program terminates im
5 min read
How to Disable Python Warnings? Python warnings are non-fatal messages that alert the user to potential problems in the code. Unlike exceptions, warnings do not interrupt the execution of the program, but they notify the user that something might not be working as expected. Warnings can be generated by the interpreter or manually
3 min read
Python | Reraise the Last Exception and Issue Warning Problem - Reraising the exception, that has been caught in the except block. Code #1: Using raise statement all by itself. Python3 1== def example(): try: int('N/A') except ValueError: print("Didn't work") raise example() Output : Didn't work Traceback (most recent call last): File "", lin
2 min read
How to Handle the Pylint Message: Warning: Method Could Be a Function The Pylint is a widely used static code analysis tool for Python that helps maintain the code quality by identifying coding errors enforcing the coding standards and offering the potential code enhancements. One common warning it generates is "Method could be a function." This article will explain t
4 min read
NO Quality Assurance (noqa) in Python Well if you are someone who has just started with Python then you would most likely be not aware of this feature. Even if you are an experienced programmer then you might not be aware of this nifty little feature. This is going to be a short and simple but very useful article. So, what exactly is no
2 min read
Learn Python Basics âPython is a versatile, high-level programming language known for its readability and simplicity. Whether you're a beginner or an experienced developer, Python offers a wide range of functionalities that make it a popular choice in various domains such as web development, data science, artificial in
9 min read