You can handle mistakes in your applications better by printing to Python's standard error stream. Check out this post to learn how to do it.
A software can communicate with its environment while it is being executed using standard streams. This can entail taking keyboard input using Python's input() function or printing a message to the screen using the print() method. Standard input, standard output, and standard error are the three standard streams in computing; they are sometimes known as stdin, stdout, and the stderr, . These streams can get access in Python using THE SYS MODULE.
Our PYTHON BASICS course will teach you the fundamentals of computer programming through interactive activities for those of you who are new to Python. Or, take a look at the track LEARN PROGRAMMING WITH PYTHON, which combines many courses.
What Is the Standard Error Stream in Python?
An output stream called STANDARD ERROR can get written to the screen or sent to a log file. This implies that any error messages can get examined to see what faults that caused. It can get redirected and unrelated to the normal output.
In Python, there are many ways to output standard errors. You will have another resource to assist you THINK LIKE A PYTHON DEVELOPER after reading our discussion of them below.
3 Python Methods for Printing to the Standard Error Stream
1. Print to stderr with print ()
The built-in print() function is the first way to output to standard error. You can refer to our article ALL YOU NEED TO KNOW ABOUT PYTHON'S PRINT() FUNCTION for further information. Here is a print() usage example for the time being:
>>> import sys
>>> print('Error message', file=sys.stderr)
To send the output to standard error in this case, we used the optional argument file. The print() function would send its result to standard output by default. You can create your own function if you wish to cut down on some typing:
>>> def print_error(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
The function can then get executed as follows:
>>> print_error('Error message')
To provide more keyword arguments to the print() statement, you can do so. You can specify what's printed at the end of the output by using the end keyword:
print_error('Error message', end=', good luck debugging me!')
Error message, good luck debugging me!
2. The second frequent way for printing to standard error in Python
It utilises sys.stderr.write rather than the print() function (). Here's an illustration:
>>> sys.stderr.write('Error message\n')
The second frequent way for printing to standard error in Python utilises sys.stderr.write rather than the print() function (). Here's an illustration:
3. Logging and Printing to stderr
The final approach makes use of the logging module, which offers features to assist you in logging events as your programmes run. This module outputs messages to standard error by default. The following is how you can import the library and log a warning:
>>> import logging
>>> log = logging.getLogger(__name__)
>>> log.warning('Error message')
Here, we instantiate a logger object using the variable __name__, set the output format, and log a warning. The result is this:
This records the error as a WARNING level entry. There are other features that offer much more capability and let you log failures at various levels. For further information, consult the PYTHON DOCUMENTATION.
Work smarter by utilising Python's Standard Error Stream.
We discussed many techniques in this post for printing to Python's standard error stream. Keep in mind that this stream is separate from conventional output. It will be less likely for your programme to crash before errors are logged if you make use of the standard error stream, as this is A DANGEROUS WAY FOR YOUR PROGRAMS TO END. Debugging software made simpler by logging mistakes.