Print error traceback python

traceback - How to Extract, Format, and Print Error Stack Traces in Python

The traceback of error generated by python interpreter can be sometimes long and not that useful. We might need to modify stack traces according to our need in some situations where we need more control over the amount of traceback getting printed. We might even need more control over the format of the trace getting printed. Python provides a module named traceback which has a list of method which let us extract error traces, format it and print it according to our need. This can help with formatting and limiting error trace getting generated. As a part of this tutorial, we’ll be explaining how we can use a different method of traceback module for different purposes. We’ll explain the usage of the method with simple examples.

There is three kinds of methods available with the traceback module.

  • print_*() — These methods are used to print stack traces to the desired output medium (standard error, standard output, file, etc).
    • print_tb()
    • print_exception()
    • print_exc()
    • print_list()
    • print_last()
    • print_stack()
  • extract_*() — These methods are used to extract stack traces from an exception and return preprocessed stack trace entries.
    • extract_tb()
    • extract_stack()
  • format_*() — These methods are used to format output generated by extract_* methods. These methods can be useful if we want to print an error stack on some GUI.
    • format_tb()
    • format_list()
    • format_exception_only()
    • format_exception()
    • format_exc()
    • format_stack()
  • walk_*() — These methods returns generator instance which lets us iterate through trace frames and print stack trace according to our need.
    • walk_stack()
    • walk_tb()

We’ll now explain the usage of these methods with simple examples one by one.

Example 1¶

As a part of our first example, we’ll explain how we can print the stack trace of error using print_tb() method.

  • print_tb(tb, limit=None, file=None) — This method accepts traceback instance and prints traces to the output. It let us limit the amount of trace getting printed by specifying limit parameter. We can give integer value to limit parameter and it’ll print trace for that many frames only. If we don’t specify a limit then by default it’ll be None and will print the whole stack trace. It has another important parameter named file which lets us specify where we want to direct output of trace to. We can give a file-like object and it’ll direct traces to it. We can also specify standard error and standard output using sys.err and sys.out as file parameters and trace will be directed to them. If we don’t specify the file parameter then by default it’ll direct trace to standard error.

Below we have artificially tried to generate an error by giving randint() method of random module negative integer numbers. We have the first printed traceback generated by the python interpreter. Then we have caught exceptions using try-except block. We have then retrieved traceback from the error object using traceback attribute of the error object and have given it to print_tb() method which prints a stack trace.

Please make a note that print_tb() only prints stack trace and does not print actual exception type.

import random

out = random.randint(-5,-10)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-a38bfad2efe7> in <module>
      1 import random
      2
----> 3 out = random.randint(-5,-10)

~/anaconda3/lib/python3.7/random.py in randint(self, a, b)
    220         """
    221
--> 222         return self.randrange(a, b+1)
    223
    224     def _randbelow(self, n, int=int, maxsize=1<<BPF, type=type,

~/anaconda3/lib/python3.7/random.py in randrange(self, start, stop, step, _int)
    198             return istart + self._randbelow(width)
    199         if step == 1:
--> 200             raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
    201
    202         # Non-unit step argument supplied.

ValueError: empty range for randrange() (-5,-9, -4)
import traceback
import random

try:
    out = random.randint(-5,-10)
except Exception as e:
    traceback.print_tb(e.__traceback__)
  File "<ipython-input-2-200195b56950>", line 5, in <module>
    out = random.randint(-5,-10)
  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 222, in randint
    return self.randrange(a, b+1)
  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 200, in randrange
    raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))

Below we have again executed code same as the previous cell but this time we have set the limit parameter to 1 hence it only prints the first trace frame.

import traceback
import random

try:
    out = random.randint(-5,-10)
except Exception as e:
    traceback.print_tb(e.__traceback__, limit=1)
  File "<ipython-input-3-6335bba4bb4d>", line 5, in <module>
    out = random.randint(-5,-10)

Example 2¶

As a part of this example, we have explained how we can direct the trace generated to a file. We have directed the error trace generated by the code to a file named traceback_ex1.out.

import traceback
import random

try:
    out = random.randint(-5,-10)
except Exception as e:
    traceback.print_tb(e.__traceback__, file=open("traceback_ex1.out", "w"))
  File "<ipython-input-4-d02890406d60>", line 5, in <module>
    out = random.randint(-5,-10)
  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 222, in randint
    return self.randrange(a, b+1)
  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 200, in randrange
    raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))

Example 3¶

As a part of this example, we have introduced two other methods print_exception() and print_exc().

  • print_exception(exception_type, value, tb, limit=None, file=None, chain=True) — This method print exception information as well as error stack trace. We need to provide it exception type, exception value, and traceback. It’ll then print exceptions and traceback. The chain parameter is a boolean flag indicating whether we should include trace for chained exceptions or not.
  • print_exc(limit=None, file=None, chain=True) — This method whenever called will print the last exception which had happened in the program.

Please make a note that parameter limit, file, and chain are present in multiple methods of traceback module and it has the same meaning in all of them.

In this example, we have called method named exc_info() of sys module that returns tuple (exception type, exception value, exception traceback). This tuple has information about a recent exception that was caught by try-except block. We have then given this tuple to print_exception() method to print an error stack trace. We have directed stack trace to standard output in all of our examples.

Please make a note that when we used print_exception() method, it printed information about exceptions as well which was not getting printed with print_tb() method.

import traceback
import random
import sys


try:
    out = random.randint(-5,-10)
except:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("==================Traceback 1 =========================")
    traceback.print_tb(exc_traceback, file=sys.stdout)

    print("n==================Traceback 2 ===================")
    traceback.print_exception(exc_type, exc_value, exc_traceback, file=sys.stdout)

    print("n==================Traceback 3 ===================")
    traceback.print_exc(file=sys.stdout)
==================Traceback 1 =========================
  File "<ipython-input-6-bf6708f55f27>", line 7, in <module>
    out = random.randint(-5,-10)
  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 222, in randint
    return self.randrange(a, b+1)
  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 200, in randrange
    raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))

==================Traceback 2 ===================
Traceback (most recent call last):
  File "<ipython-input-6-bf6708f55f27>", line 7, in <module>
    out = random.randint(-5,-10)
  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 222, in randint
    return self.randrange(a, b+1)
  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 200, in randrange
    raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (-5,-9, -4)

==================Traceback 3 ===================
Traceback (most recent call last):
  File "<ipython-input-6-bf6708f55f27>", line 7, in <module>
    out = random.randint(-5,-10)
  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 222, in randint
    return self.randrange(a, b+1)
  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 200, in randrange
    raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (-5,-9, -4)

Example 4¶

As a part of our fourth example, we have demonstrated how we can catch the exception and print stack trace using print_exception() method. We have not retrieved exception information using sys.exc_info() this time. We have artificially generated divide by zero error in this example.

import traceback
import random

try:
    out = 10/0
except Exception as e:
    traceback.print_exception(type(e), e, e.__traceback__, file=sys.stdout)
Traceback (most recent call last):
  File "<ipython-input-7-c90920d6446e>", line 5, in <module>
    out = 10/0
ZeroDivisionError: division by zero

Example 5¶

We’ll use our fifth example to demonstrate the usage of print_last() method.

  • print_last(limit=None, file=None, chain=True) — This method prints last exception which had happened.

We have first artificially generated error and then printed the error stack by calling print_last() method.

---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-8-ff29a4977585> in <module>
----> 1 out = 10/0

ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "/home/sunny/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3418, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-8-ff29a4977585>", line 1, in <module>
    out = 10/0
ZeroDivisionError: division by zero

Example 6¶

As a part of our sixth example, we’ll demonstrate usage of extract_tb(), format_list() and print_list() methods.

  • extract_tb(tb, limit=None) — This method accepts traceback object and returns StackSummary instance. The StackSummary instance has list of FrameSummary instance where each FrameSummary instance has information about one frame of stack trace.
  • format_list(extracted_list) — This method takes as input StackSummary instance or list of FrameSummary instances as input and returns a list of strings representing stack trace.
  • print_list(extracted_list, file=None) — This method takes as input StackSummary instance or list of FrameSummary instances as input and prints stack trace.

Below we have first extracted the error stack using extract_tb() method and then printed it in different formats. We have also explained how we can use format_list() and print_list() methods. The StackSummary instance also has format() method which works exactly like format_list() method.

import traceback
import random
import sys

try:
    out = random.randint(-5,-10)
except:
    exc_type, exc_value, exc_traceback = sys.exc_info()

    traces = traceback.extract_tb(exc_traceback)

    print("Type of Traceback         : ", type(traces))
    print("Type of Individual Traces : ", type(traces[0]))

    print(" n========== Trace Format 1 ===============n")
    print("%50s | %10s | %5s | %10s" %("File Name", "Method Name", "Line Number", "Line"))
    print("-"*100)
    for frame_summary in traces:
        print("%50s | %11s | %11d | %10s"%(frame_summary.filename, frame_summary.name, frame_summary.lineno, frame_summary.line))
        print("-"*100)

    print(" n========== Trace Format 2 ===============n")
    for trace_line in traces.format():
        print(trace_line)

    print(" n========== Trace Format 3 ===============n")
    for trace_line in traceback.format_list(traces):
        print(trace_line)

    print(" n========== Trace Format 4 ===============n")
    traceback.print_list(traces, file=sys.stdout)
Type of Traceback         :  <class 'traceback.StackSummary'>
Type of Individual Traces :  <class 'traceback.FrameSummary'>

========== Trace Format 1 ===============

                                         File Name | Method Name | Line Number |       Line
----------------------------------------------------------------------------------------------------
                   <ipython-input-10-e08cb7446344> |    <module> |           6 | out = random.randint(-5,-10)
----------------------------------------------------------------------------------------------------
     /home/sunny/anaconda3/lib/python3.7/random.py |     randint |         222 | return self.randrange(a, b+1)
----------------------------------------------------------------------------------------------------
     /home/sunny/anaconda3/lib/python3.7/random.py |   randrange |         200 | raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
----------------------------------------------------------------------------------------------------

========== Trace Format 2 ===============

  File "<ipython-input-10-e08cb7446344>", line 6, in <module>
    out = random.randint(-5,-10)

  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 222, in randint
    return self.randrange(a, b+1)

  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 200, in randrange
    raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))


========== Trace Format 3 ===============

  File "<ipython-input-10-e08cb7446344>", line 6, in <module>
    out = random.randint(-5,-10)

  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 222, in randint
    return self.randrange(a, b+1)

  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 200, in randrange
    raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))


========== Trace Format 4 ===============

  File "<ipython-input-10-e08cb7446344>", line 6, in <module>
    out = random.randint(-5,-10)
  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 222, in randint
    return self.randrange(a, b+1)
  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 200, in randrange
    raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))

Example 7¶

As a part of our seventh example, we’ll explain usage of methods format_tb(), format_exception() and format_exc().

  • format_tb() — This method works exactly the same as print_tb() method with the only difference that it returns a list of strings where each string is a single trace of the stack.
  • format_exception() — This method works exactly the same as print_exception() method with the only difference that it returns a list of strings where each string is a single trace of the stack.
  • format_exc() — This method works exactly the same as print_exc() method with the only difference that it returns a string that has stack trace.

Below we have explained how we can use these methods with a simple example.

import traceback
import random
import sys


try:
    out = random.randint(-5,-10)
except:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("==================Traceback 1 =========================")
    for trace in traceback.format_tb(exc_traceback):
        print(trace)

    print("n==================Traceback 2 ===================")
    for trace in traceback.format_exception(exc_type, exc_value, exc_traceback):
        print(trace)

    print("n==================Traceback 3 ===================")
    print(traceback.format_exc())
==================Traceback 1 =========================
  File "<ipython-input-11-cd6fba9720d8>", line 7, in <module>
    out = random.randint(-5,-10)

  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 222, in randint
    return self.randrange(a, b+1)

  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 200, in randrange
    raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))


==================Traceback 2 ===================
Traceback (most recent call last):

  File "<ipython-input-11-cd6fba9720d8>", line 7, in <module>
    out = random.randint(-5,-10)

  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 222, in randint
    return self.randrange(a, b+1)

  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 200, in randrange
    raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))

ValueError: empty range for randrange() (-5,-9, -4)


==================Traceback 3 ===================
Traceback (most recent call last):
  File "<ipython-input-11-cd6fba9720d8>", line 7, in <module>
    out = random.randint(-5,-10)
  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 222, in randint
    return self.randrange(a, b+1)
  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 200, in randrange
    raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (-5,-9, -4)

Example 8¶

As a part of our eighth example, we’ll demonstrate the usage of format_exception_only() method.

  • format_exception_only(exception_type, exception_value) — This method takes as input exception type and exception value. It then return list of string specifying exception description.

Below we have explained the usage of it with simple example.

import traceback
import random

try:
    out = 10/0
except Exception as e:
    for exception_line in traceback.format_exception_only(type(e), e):
        print(exception_line)
ZeroDivisionError: division by zero

Example 9¶

As a part of our ninth example, we’ll demonstrate the usage of walk_tb() and StackSummary.extract() methods.

  • walk_tb(tb) — This method takes as input the traceback instance and returns a generator that has a list of frames of the stack trace.
  • StackSummary.extract() — This method takes as input frame generator which has stack trace and generates StackSummary from it.

The StackSummary class has another method named from_list(frame_summary_list) which takes as an input list of FrameSummary instances and generates StackSummary instance from it. It can also take a list of tuples (filename, line no, name, line) as input to generate the StackSummary instance.

Below we have explained how we can generate StackSummary instance from the frame generator generated by walk_tb() method. We have then formatted stack trace as well using the StackSummary instance.

import traceback
import random
import sys

try:
    out = random.randint(-5,-10)
except Exception as e:
    tbk = e.__traceback__
    print("n==================Traceback 1 ===================n")
    for frame in traceback.walk_tb(tbk):
            print(frame)

    print("n==================Traceback 2 ===================n")
    stack_summary = traceback.StackSummary.extract(traceback.walk_tb(tbk))

    print("%50s | %10s | %5s | %10s" %("File Name", "Method Name", "Line Number", "Line"))
    print("-"*100)

    for frame_summary in stack_summary:
        print("%50s | %11s | %11d | %10s"%(frame_summary.filename, frame_summary.name, frame_summary.lineno, frame_summary.line))
        print("-"*100)
==================Traceback 1 ===================

(<frame at 0x7faebc361248, file '<ipython-input-13-3a3aaaa264bb>', line 11, code <module>>, 6)
(<frame at 0x7faebc2e9768, file '/home/sunny/anaconda3/lib/python3.7/random.py', line 222, code randint>, 222)
(<frame at 0x7faebc2fb248, file '/home/sunny/anaconda3/lib/python3.7/random.py', line 200, code randrange>, 200)

==================Traceback 2 ===================

                                         File Name | Method Name | Line Number |       Line
----------------------------------------------------------------------------------------------------
                   <ipython-input-13-3a3aaaa264bb> |    <module> |           6 | out = random.randint(-5,-10)
----------------------------------------------------------------------------------------------------
     /home/sunny/anaconda3/lib/python3.7/random.py |     randint |         222 | return self.randrange(a, b+1)
----------------------------------------------------------------------------------------------------
     /home/sunny/anaconda3/lib/python3.7/random.py |   randrange |         200 | raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
----------------------------------------------------------------------------------------------------

Example 10¶

As a part of our tenth example, we have demonstrated the usage of print_stack() method.

  • print_stack(f=None, limit=None, file=None) — This method takes as input frame instance and prints trace back from that frame onwards.

Below we have explained how we can use print_stack() method.

import traceback
import random
import sys

try:
    out = random.randint(-5,-10)
except Exception as e:
    tbk = e.__traceback__

    while tbk:
        print("Line No : ", tbk.tb_lineno)
        #print("Frame : ", tbk.tb_frame)
        print("========== Trace ==========")
        traceback.print_stack(tbk.tb_frame, limit=1, file=sys.stdout)
        print()
        tbk = tbk.tb_next
Line No :  6
========== Trace ==========
  File "<ipython-input-14-240546dff3ae>", line 14, in <module>
    traceback.print_stack(tbk.tb_frame, limit=1, file=sys.stdout)

Line No :  222
========== Trace ==========
  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 222, in randint
    return self.randrange(a, b+1)

Line No :  200
========== Trace ==========
  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 200, in randrange
    raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))

Example 11¶

As a part of our eleventh example, we have demonstrated the usage of format_stack() method.

  • format_stack(f=None, limit=None) — This method is exactly the same as print_stack() method with the only difference that it returns a list of strings instead of printing the stack trace.

Below we have explained the usage of format_stack() with a simple example that has code almost the same as print_stack() with minor changes.

import traceback
import random
import sys

try:
    out = random.randint(-5,-10)
except Exception as e:
    tbk = e.__traceback__

    while tbk:
        print("Line No : ", tbk.tb_lineno)
        #print("Frame : ", tbk.tb_frame)
        print("========== Trace ==========")
        print(traceback.format_stack(tbk.tb_frame, limit=1)[0])
        print()
        tbk = tbk.tb_next
Line No :  6
========== Trace ==========
  File "<ipython-input-15-f2267a4aafa9>", line 14, in <module>
    print(traceback.format_stack(tbk.tb_frame, limit=1)[0])


Line No :  222
========== Trace ==========
  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 222, in randint
    return self.randrange(a, b+1)


Line No :  200
========== Trace ==========
  File "/home/sunny/anaconda3/lib/python3.7/random.py", line 200, in randrange
    raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))


Example 12¶

As a part of our twelfth example, we have demonstrated the usage of extract_stack() method.

  • extract_stack(f=None, limit=None) — This method works exactly print_stack() with only difference that it returns list of FrameSummary instance rather than printing stack trace.

We have explained the usage of extract_stack() with a simple example below. We can even generate StackSummary instance from the output of extract_stack() method.

import traceback
import random
import sys

try:
    out = random.randint(-5,-10)
except Exception as e:
    tbk = e.__traceback__

    print("%50s | %10s | %5s | %10s" %("File Name", "Method Name", "Line Number", "Line"))
    print("-"*100)

    while tbk:
        trace = list(traceback.extract_stack(tbk.tb_frame, limit=1))[0]
        print("%50s | %11s | %11d | %10s"%(trace.filename, trace.name, trace.lineno, trace.line))
        print("-"*100)

        tbk = tbk.tb_next
                                         File Name | Method Name | Line Number |       Line
----------------------------------------------------------------------------------------------------
                   <ipython-input-16-181d69cd0b74> |    <module> |          14 | trace = list(traceback.extract_stack(tbk.tb_frame, limit=1))[0]
----------------------------------------------------------------------------------------------------
     /home/sunny/anaconda3/lib/python3.7/random.py |     randint |         222 | return self.randrange(a, b+1)
----------------------------------------------------------------------------------------------------
     /home/sunny/anaconda3/lib/python3.7/random.py |   randrange |         200 | raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
----------------------------------------------------------------------------------------------------

Example 13¶

As a part of our thirteenth and last example, we’ll demonstrate the usage of walk_stack() method.

  • walk_stack(f) — This method works exactly like extract_stack() method but returns generator of stack trace frames. We can generate a StackSummary instance from the output of this method.

Below we have explained the usage of the method with simple example. We have first generated StackSummary instance from the output of walk_stack() method. We have then formatted the stack trace.

import traceback
import random
import sys

try:
    out = random.randint(-5,-10)
except Exception as e:
    tbk = e.__traceback__

    print("%50s | %10s | %5s | %10s" %("File Name", "Method Name", "Line Number", "Line"))
    print("-"*100)
    while tbk:
        stack_summary  = traceback.StackSummary.extract(traceback.walk_stack(tbk.tb_frame))

        for frame_summary in stack_summary[:1]:
            print("%50s | %11s | %11d | %10s"%(frame_summary.filename, frame_summary.name, frame_summary.lineno, frame_summary.line))
            print("-"*100)

        tbk = tbk.tb_next

                                         File Name | Method Name | Line Number |       Line
----------------------------------------------------------------------------------------------------
                   <ipython-input-17-ce4b3126d8ff> |    <module> |          13 | stack_summary  = traceback.StackSummary.extract(traceback.walk_stack(tbk.tb_frame))
----------------------------------------------------------------------------------------------------
     /home/sunny/anaconda3/lib/python3.7/random.py |     randint |         222 | return self.randrange(a, b+1)
----------------------------------------------------------------------------------------------------
     /home/sunny/anaconda3/lib/python3.7/random.py |   randrange |         200 | raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
----------------------------------------------------------------------------------------------------

This ends our small tutorial explaining the usage of the traceback module. Please feel free to let us know your views in the comments section.

Печать или вывод в файл информации об исключениях и записей трассировки стека.

Содержание:

  • traceback.print_tb() -печать трассировки стека,
  • traceback.print_exception() — печать исключения и трассировку стека,
  • traceback.print_exc() — сокращение для вызова traceback.print_exception(),
  • traceback.print_last() — еще одно сокращение для вызова print_exception(),
  • traceback.print_stack() — печатает записи трассировки, начиная с точки вызова,
  • traceback.format_exc() — похожа на traceback.print_exc(limit) но возвращает строку вместо печати,
  • Примеры использования вывода на печать исключений и трассировки стека.

traceback.print_tb(tb, limit=None, file=None):

Функция traceback.print_tb() если аргумент limit положительный, то печатает записи трассировки стека из объекта трассировки tb, ограничивая количество записей значением limit (начиная с кадра вызывающего абонента). В противном случае выводит последние записи abs(limit).

  • Если аргумент limit опущен или отсутствует, то печатаются все записи.
  • Если аргумент файла file опущен или отсутствует, то вывод идет в sys.stderr.
  • Если аргумент файла file задан, то для получения вывода он должен быть открытым файлом или файлоподобным объектом.

traceback.print_exception(etype, value, tb, limit=None, file=None, chain=True):

Функция traceback.print_exception() выводит информацию об исключении и записи трассировки стека из объекта трассировки tb в файл.

Поведение traceback.print_exception() отличается от traceback.print_tb() следующим образом:

  • Если tb не равно None, то выводится заголовок Traceback (most recent call last):,
  • Функция печатает etype исключения и значение после трассировки стека,
  • Если type(value) — это SyntaxError, а значение имеет соответствующий формат, то функция печатает строку, в которой произошла синтаксическая ошибка с символом вставки, указывающим приблизительное положение ошибки.

Необязательный аргумент limit имеет то же значение, что и в функции traceback.print_tb().

Если аргумент chain=True (по умолчанию), то связанные исключения (атрибуты исключения __cause__ или __context__) также будут выведены, как это делает сам интерпретатор при печати необработанного исключения.

traceback.print_exc(limit=None, file=None, chain=True):

Функция traceback.print_exc() представляет собой сокращенное название для вызова traceback.print_exception() и вызывается с параметрами:

traceback.print_exception(*sys.exc_info(), limit, file, chain)
Обратите внимание

, что значение аргументов etype, value и tb функции traceback.print_exception() уже подставлены в виде вызова sys.exc_info().

traceback.print_last(limit=None, file=None, chain=True):

Функция traceback.print_last() представляет собой сокращенный для вызова traceback.print_exception() и вызывается с параметрами:

traceback.print_exception(sys.last_type, sys.last_value, sys.last_traceback, limit, file, chain)

Как правило, функция будет работать только после того, как исключение достигнет интерактивной подсказки (подробнее смотрите описание sys.last_type).

Обратите внимание

, что значение аргументов etype, value и tb функции traceback.print_exception() уже подставлены в виде значений sys.last_type, sys.last_value и sys.last_traceback соответственно.

traceback.print_stack(f=None, limit=None, file=None):

Функция traceback.print_stack() если limit является положительным, то печатает записи трассировки стека, начиная с точки вызова, ограничивая количество значением limit. В противном случае выводит на печать последние записи abs(limit).

  • Если аргумент limit опущен или отсутствует, то печатаются все записи.
  • Необязательный аргумент f можно использовать для указания альтернативного кадра стека для запуска.
  • Необязательный аргумент file имеет то же значение, что и для функции traceback.print_tb().

traceback.format_exc(limit=None, chain=True):

Функция traceback.format_exc() похожа на traceback.print_exc(limit) но возвращает строку вместо печати или сохранения в файл.

Примеры использования вывода на печать исключений и трассировки стека:

Пример наглядно демонстрирует различные способы печати исключения и обратной трассировки:

# test.py
import sys, traceback

def lumberjack():
    bright_side_of_death()

def bright_side_of_death():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("*** print_tb:")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print("*** print_exception:")
    # exc_type below is ignored on 3.5 and later
    traceback.print_exception(exc_type, exc_value, exc_traceback,
                              limit=2, file=sys.stdout)
    print("*** print_exc:")
    traceback.print_exc(limit=2, file=sys.stdout)
    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])

Результаты вывода:

$ python3 test.py
*** print_tb:
  File "<test...>", line 10, in <module>
    lumberjack()
*** print_exception:
Traceback (most recent call last):
  File "<test...>", line 10, in <module>
    lumberjack()
  File "<test...>", line 4, in lumberjack
    bright_side_of_death()
IndexError: tuple index out of range
*** print_exc:
Traceback (most recent call last):
  File "<test...>", line 10, in <module>
    lumberjack()
  File "<test...>", line 4, in lumberjack
    bright_side_of_death()
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
``

Python выводит трассировку (далее traceback), когда в вашем коде появляется ошибка. Вывод traceback может быть немного пугающим, если вы видите его впервые, или не понимаете, чего от вас хотят. Однако traceback Python содержит много информации, которая может помочь вам определить и исправить причину, из-за которой в вашем коде возникла ошибка.

Содержание статьи

  • Traceback — Что это такое и почему оно появляется?
  • Как правильно читать трассировку?
  • Обзор трассировка Python
  • Подробный обзор трассировки в Python
  • Обзор основных Traceback исключений в Python
  • AttributeError
  • ImportError
  • IndexError
  • KeyError
  • NameError
  • SyntaxError
  • TypeError
  • ValueError
  • Логирование ошибок из Traceback
  • Вывод

Понимание того, какую информацию предоставляет traceback Python является основополагающим критерием того, как стать лучшим Python программистом.

К концу данной статьи вы сможете:

  • Понимать, что несет за собой traceback
  • Различать основные виды traceback
  • Успешно вести журнал traceback, при этом исправить ошибку

Python Traceback — Как правильно читать трассировку?

Traceback (трассировка) — это отчет, который содержит вызовы выполненных функций в вашем коде в определенный момент.

Есть вопросы по Python?

На нашем форуме вы можете задать любой вопрос и получить ответ от всего нашего сообщества!

Telegram Чат & Канал

Вступите в наш дружный чат по Python и начните общение с единомышленниками! Станьте частью большого сообщества!

Паблик VK

Одно из самых больших сообществ по Python в социальной сети ВК. Видео уроки и книги для вас!

Traceback называют по разному, иногда они упоминаются как трассировка стэка, обратная трассировка, и так далее. В Python используется определение “трассировка”.

Когда ваша программа выдает ошибку, Python выводит текущую трассировку, чтобы подсказать вам, что именно пошло не так. Ниже вы увидите пример, демонстрирующий данную ситуацию:

def say_hello(man):

    print(‘Привет, ‘ + wrong_variable)

say_hello(‘Иван’)

Здесь say_hello() вызывается с параметром man. Однако, в say_hello() это имя переменной не используется. Это связано с тем, что оно написано по другому: wrong_variable в вызове print().

Обратите внимание: в данной статье подразумевается, что вы уже имеете представление об ошибках Python. Если это вам не знакомо, или вы хотите освежить память, можете ознакомиться с нашей статьей: Обработка ошибок в Python

Когда вы запускаете эту программу, вы получите следующую трассировку:

Traceback (most recent call last):

  File «/home/test.py», line 4, in <module>

    say_hello(‘Иван’)

  File «/home/test.py», line 2, in say_hello

    print(‘Привет, ‘ + wrong_variable)

NameError: name ‘wrong_variable’ is not defined

Process finished with exit code 1

Эта выдача из traceback содержит массу информации, которая вам понадобится для определения проблемы. Последняя строка трассировки говорит нам, какой тип ошибки возник, а также дополнительная релевантная информация об ошибке. Предыдущие строки из traceback указывают на код, из-за которого возникла ошибка.

В traceback выше, ошибкой является NameError, она означает, что есть отсылка к какому-то имени (переменной, функции, класса), которое не было определено. В данном случае, ссылаются на имя wrong_variable.

Последняя строка содержит достаточно информации для того, чтобы вы могли решить эту проблему. Поиск переменной wrong_variable, и заменит её атрибутом из функции на man. Однако, скорее всего в реальном случае вы будете иметь дело с более сложным кодом.

Python Traceback — Как правильно понять в чем ошибка?

Трассировка Python содержит массу полезной информации, когда вам нужно определить причину ошибки, возникшей в вашем коде. В данном разделе, мы рассмотрим различные виды traceback, чтобы понять ключевые отличия информации, содержащейся в traceback.

Существует несколько секций для каждой трассировки Python, которые являются крайне важными. Диаграмма ниже описывает несколько частей:

Обзор трассировки Python

В Python лучше всего читать трассировку снизу вверх.

  1. Синее поле: последняя строка из traceback — это строка уведомления об ошибке. Синий фрагмент содержит название возникшей ошибки.
  2. Зеленое поле: после названия ошибки идет описание ошибки. Это описание обычно содержит полезную информацию для понимания причины возникновения ошибки.
  3. Желтое поле: чуть выше в трассировке содержатся различные вызовы функций. Снизу вверх — от самых последних, до самых первых. Эти вызовы представлены двухстрочными вводами для каждого вызова. Первая строка каждого вызова содержит такую информацию, как название файла, номер строки и название модуля. Все они указывают на то, где может быть найден код.
  4. Красное подчеркивание: вторая строка этих вызовов содержит непосредственный код, который был выполнен с ошибкой.

Есть ряд отличий между выдачей трассировок, когда вы запускает код в командной строке, и между запуском кода в REPL. Ниже вы можете видеть тот же код из предыдущего раздела, запущенного в REPL и итоговой выдачей трассировки:

Python 3.7.4 (default, Jul 16 2019, 07:12:58)

[GCC 9.1.0] on linux

Type «help», «copyright», «credits» or «license» for more information.

>>>

>>>

>>> def say_hello(man):

...     print(‘Привет, ‘ + wrong_variable)

...

>>> say_hello(‘Иван’)

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

  File «<stdin>», line 2, in say_hello

NameError: name ‘wrong_variable’ is not defined

Обратите внимание на то, что на месте названия файла вы увидите <stdin>. Это логично, так как вы выполнили код через стандартный ввод. Кроме этого, выполненные строки кода не отображаются в traceback.

Важно помнить: если вы привыкли видеть трассировки стэка в других языках программирования, то вы обратите внимание на явное различие с тем, как выглядит traceback в Python. Большая часть других языков программирования выводят ошибку в начале, и затем ведут сверху вниз, от недавних к последним вызовам.

Это уже обсуждалось, но все же: трассировки Python читаются снизу вверх. Это очень помогает, так как трассировка выводится в вашем терминале (или любым другим способом, которым вы читаете трассировку) и заканчивается в конце выдачи, что помогает последовательно структурировать прочтение из traceback и понять в чем ошибка.

Traceback в Python на примерах кода

Изучение отдельно взятой трассировки поможет вам лучше понять и увидеть, какая информация в ней вам дана и как её применить.

Код ниже используется в примерах для иллюстрации информации, данной в трассировке Python:

Мы запустили ниже предоставленный код в качестве примера и покажем какую информацию мы получили от трассировки.

Сохраняем данный код в файле greetings.py

def who_to_greet(person):

    return person if person else input(‘Кого приветствовать? ‘)

def greet(someone, greeting=‘Здравствуйте’):

    print(greeting + ‘, ‘ + who_to_greet(someone))

def greet_many(people):

    for person in people:

        try:

            greet(person)

        except Exception:

            print(‘Привет, ‘ + person)

Функция who_to_greet() принимает значение person и либо возвращает данное значение если оно не пустое, либо запрашивает  значение от пользовательского ввода через input().

Далее, greet() берет имя для приветствия из someone, необязательное значение из greeting и вызывает print(). Также с переданным значением из someone вызывается who_to_greet().

Наконец, greet_many() выполнит итерацию по списку людей и вызовет greet(). Если при вызове greet() возникает ошибка, то выводится резервное приветствие print('hi, ' + person).

Этот код написан правильно, так что никаких ошибок быть не может при наличии правильного ввода.

Если вы добавите вызов функции greet() в конце нашего кода (которого сохранили в файл greetings.py) и дадите аргумент который он не ожидает (например, greet('Chad', greting='Хай')), то вы получите следующую трассировку:

$ python greetings.py

Traceback (most recent call last):

  File «/home/greetings.py», line 19, in <module>

    greet(‘Chad’, greting=‘Yo’)

TypeError: greet() got an unexpected keyword argument ‘greting’

Еще раз, в случае с трассировкой Python, лучше анализировать снизу вверх. Начиная с последней строки трассировки, вы увидите, что ошибкой является TypeError. Сообщения, которые следуют за типом ошибки, дают вам полезную информацию. Трассировка сообщает, что greet() вызван с аргументом, который не ожидался. Неизвестное название аргумента предоставляется в том числе, в нашем случае это greting.

Поднимаясь выше, вы можете видеть строку, которая привела к исключению. В данном случае, это вызов greet(), который мы добавили в конце greetings.py.

Следующая строка дает нам путь к файлу, в котором лежит код, номер строки этого файла, где вы можете найти код, и то, какой в нем модуль. В нашем случае, так как наш код не содержит никаких модулей Python, мы увидим только надпись , означающую, что этот файл является выполняемым.

С другим файлом и другим вводом, вы можете увидеть, что трассировка явно указывает вам на правильное направление, чтобы найти проблему. Следуя этой информации, мы удаляем злополучный вызов greet() в конце greetings.py, и добавляем следующий файл под названием example.py в папку:

from greetings import greet

greet(1)

Здесь вы настраиваете еще один файл Python, который импортирует ваш предыдущий модуль greetings.py, и используете его greet(). Вот что произойдете, если вы запустите example.py:

$ python example.py

Traceback (most recent call last):

  File «/path/to/example.py», line 3, in <module>

    greet(1)

  File «/path/to/greetings.py», line 5, in greet

    print(greeting + ‘, ‘ + who_to_greet(someone))

TypeError: must be str, not int

В данном случае снова возникает ошибка TypeError, но на этот раз уведомление об ошибки не очень помогает. Оно говорит о том, что где-то в коде ожидается работа со строкой, но было дано целое число.

Идя выше, вы увидите строку кода, которая выполняется. Затем файл и номер строки кода. На этот раз мы получаем имя функции, которая была выполнена — greet().

Поднимаясь к следующей выполняемой строке кода, мы видим наш проблемный вызов greet(), передающий целое число.

Иногда, после появления ошибки, другой кусок кода берет эту ошибку и также её выдает. В таких случаях, Python выдает все трассировки ошибки в том порядке, в котором они были получены, и все по тому же принципу, заканчивая на самой последней трассировке.

Так как это может сбивать с толку, рассмотрим пример. Добавим вызов greet_many() в конце greetings.py:

# greetings.py

...

greet_many([‘Chad’, ‘Dan’, 1])

Это должно привести к выводу приветствия всем трем людям. Однако, если вы запустите этот код, вы увидите несколько трассировок в выдаче:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

$ python greetings.py

Hello, Chad

Hello, Dan

Traceback (most recent call last):

  File «greetings.py», line 10, in greet_many

    greet(person)

  File «greetings.py», line 5, in greet

    print(greeting + ‘, ‘ + who_to_greet(someone))

TypeError: must be str, not int

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File «greetings.py», line 14, in <module>

    greet_many([‘Chad’, ‘Dan’, 1])

  File «greetings.py», line 12, in greet_many

    print(‘hi, ‘ + person)

TypeError: must be str, not int

Обратите внимание на выделенную строку, начинающуюся с “During handling in the output above”. Между всеми трассировками, вы ее увидите.

Это достаточно ясное уведомление: Пока ваш код пытался обработать предыдущую ошибку, возникла новая.

Обратите внимание: функция отображения предыдущих трассировок была добавлена в Python 3. В Python 2 вы можете получать только трассировку последней ошибки.

Вы могли видеть предыдущую ошибку, когда вызывали greet() с целым числом. Так как мы добавили 1 в список людей для приветствия, мы можем ожидать тот же результат. Однако, функция greet_many() оборачивает вызов greet() и пытается в блоке try и except. На случай, если greet() приведет к ошибке, greet_many() захочет вывести приветствие по-умолчанию.

Соответствующая часть greetings.py повторяется здесь:

def greet_many(people):

    for person in people:

        try:

            greet(person)

        except Exception:

            print(‘hi, ‘ + person)

Когда greet() приводит к TypeError из-за неправильного ввода числа, greet_many() обрабатывает эту ошибку и пытается вывести простое приветствие. Здесь код приводит к другой, аналогичной ошибке. Он все еще пытается добавить строку и целое число.

Просмотр всей трассировки может помочь вам увидеть, что стало причиной ошибки. Иногда, когда вы получаете последнюю ошибку с последующей трассировкой, вы можете не увидеть, что пошло не так. В этих случаях, изучение предыдущих ошибок даст лучшее представление о корне проблемы.

Обзор основных Traceback исключений в Python 3

Понимание того, как читаются трассировки Python, когда ваша программа выдает ошибку, может быть очень полезным навыком, однако умение различать отдельные трассировки может заметно ускорить вашу работу.

Рассмотрим основные ошибки, с которыми вы можете сталкиваться, причины их появления и что они значат, а также информацию, которую вы можете найти в их трассировках.

Ошибка AttributeError object has no attribute [Решено]

AttributeError возникает тогда, когда вы пытаетесь получить доступ к атрибуту объекта, который не содержит определенного атрибута. Документация Python определяет, когда эта ошибка возникнет:

Возникает при вызове несуществующего атрибута или присвоение значения несуществующему атрибуту.

Пример ошибки AttributeError:

>>> an_int = 1

>>> an_int.an_attribute

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

AttributeError: ‘int’ object has no attribute ‘an_attribute’

Строка уведомления об ошибке для AttributeError говорит вам, что определенный тип объекта, в данном случае int, не имеет доступа к атрибуту, в нашем случае an_attribute. Увидев AttributeError в строке уведомления об ошибке, вы можете быстро определить, к какому атрибуту вы пытались получить доступ, и куда перейти, чтобы это исправить.

Большую часть времени, получение этой ошибки определяет, что вы возможно работаете с объектом, тип которого не является ожидаемым:

>>> a_list = (1, 2)

>>> a_list.append(3)

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

AttributeError: ‘tuple’ object has no attribute ‘append’

В примере выше, вы можете ожидать, что a_list будет типом списка, который содержит метод .append(). Когда вы получаете ошибку AttributeError, и видите, что она возникла при попытке вызова .append(), это говорит о том, что вы, возможно, не работаете с типом объекта, который ожидаете.

Часто это происходит тогда, когда вы ожидаете, что объект вернется из вызова функции или метода и будет принадлежать к определенному типу, но вы получаете тип объекта None. В данном случае, строка уведомления об ошибке будет выглядеть так:

AttributeError: ‘NoneType’ object has no attribute ‘append’

Python Ошибка ImportError: No module named [Решено]

ImportError возникает, когда что-то идет не так с оператором import. Вы получите эту ошибку, или ее подкласс ModuleNotFoundError, если модуль, который вы хотите импортировать, не может быть найден, или если вы пытаетесь импортировать что-то, чего не существует во взятом модуле. Документация Python определяет, когда возникает эта ошибка:

Ошибка появляется, когда в операторе импорта возникают проблемы при попытке загрузить модуль. Также вызывается, при конструкции импорта from list в from ... import имеет имя, которое невозможно найти.

Вот пример появления ImportError и ModuleNotFoundError:

>>> import asdf

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

ModuleNotFoundError: No module named ‘asdf’

>>> from collections import asdf

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

ImportError: cannot import name ‘asdf’

В примере выше, вы можете видеть, что попытка импорта модуля asdf, который не существует, приводит к ModuleNotFoundError. При попытке импорта того, что не существует (в нашем случае — asdf) из модуля, который существует (в нашем случае — collections), приводит к ImportError. Строки сообщения об ошибке трассировок указывают на то, какая вещь не может быть импортирована, в обоих случаях это asdf.

Ошибка IndexError: list index out of range [Решено]

IndexError возникает тогда, когда вы пытаетесь вернуть индекс из последовательности, такой как список или кортеж, и при этом индекс не может быть найден в последовательности. Документация Python определяет, где эта ошибка появляется:

Возникает, когда индекс последовательности находится вне диапазона.

Вот пример, который приводит к IndexError:

>>> a_list = [‘a’, ‘b’]

>>> a_list[3]

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

IndexError: list index out of range

Строка сообщения об ошибке для IndexError не дает вам полную информацию. Вы можете видеть, что у вас есть отсылка к последовательности, которая не доступна и то, какой тип последовательности рассматривается, в данном случае это список.

Иными словами, в списке a_list нет значения с ключом 3. Есть только значение с ключами 0 и 1, это a и b соответственно.

Эта информация, в сочетании с остальной трассировкой, обычно является исчерпывающей для помощи программисту в быстром решении проблемы.

Возникает ошибка KeyError в Python 3 [Решено]

Как и в случае с IndexError, KeyError возникает, когда вы пытаетесь получить доступ к ключу, который отсутствует в отображении, как правило, это dict. Вы можете рассматривать его как IndexError, но для словарей. Из документации:

Возникает, когда ключ словаря не найден в наборе существующих ключей.

Вот пример появления ошибки KeyError:

>>> a_dict = [‘a’: 1, ‘w’: ‘2’]

>>> a_dict[‘b’]

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

KeyError: ‘b’

Строка уведомления об ошибки KeyError говорит о ключе, который не может быть найден. Этого не то чтобы достаточно, но, если взять остальную часть трассировки, то у вас будет достаточно информации для решения проблемы.

Ошибка NameError: name is not defined в Python [Решено]

NameError возникает, когда вы ссылаетесь на название переменной, модуля, класса, функции, и прочего, которое не определено в вашем коде.

Документация Python дает понять, когда возникает эта ошибка NameError:

Возникает, когда локальное или глобальное название не было найдено.

В коде ниже, greet() берет параметр person. Но в самой функции, этот параметр был назван с ошибкой, persn:

>>> def greet(person):

...     print(f‘Hello, {persn}’)

>>> greet(‘World’)

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

  File «<stdin>», line 2, in greet

NameError: name ‘persn’ is not defined

Строка уведомления об ошибке трассировки NameError указывает вам на название, которое мы ищем. В примере выше, это названная с ошибкой переменная или параметр функции, которые были ей переданы.

NameError также возникнет, если берется параметр, который мы назвали неправильно:

>>> def greet(persn):

...     print(f‘Hello, {person}’)

>>> greet(‘World’)

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

  File «<stdin>», line 2, in greet

NameError: name ‘person’ is not defined

Здесь все выглядит так, будто вы сделали все правильно. Последняя строка, которая была выполнена, и на которую ссылается трассировка выглядит хорошо.

Если вы окажетесь в такой ситуации, то стоит пройтись по коду и найти, где переменная person была использована и определена. Так вы быстро увидите, что название параметра введено с ошибкой.

Ошибка SyntaxError: invalid syntax в Python [Решено]

Возникает, когда синтаксический анализатор обнаруживает синтаксическую ошибку.

Ниже, проблема заключается в отсутствии двоеточия, которое должно находиться в конце строки определения функции. В REPL Python, эта ошибка синтаксиса возникает сразу после нажатия Enter:

>>> def greet(person)

  File «<stdin>», line 1

    def greet(person)

                    ^

SyntaxError: invalid syntax

Строка уведомления об ошибке SyntaxError говорит вам только, что есть проблема с синтаксисом вашего кода. Просмотр строк выше укажет вам на строку с проблемой. Каретка ^ обычно указывает на проблемное место. В нашем случае, это отсутствие двоеточия в операторе def нашей функции.

Стоит отметить, что в случае с трассировками SyntaxError, привычная первая строка Tracebak (самый последний вызов) отсутствует. Это происходит из-за того, что SyntaxError возникает, когда Python пытается парсить ваш код, но строки фактически не выполняются.

Ошибка TypeError в Python 3 [Решено]

TypeError возникает, когда ваш код пытается сделать что-либо с объектом, который не может этого выполнить, например, попытка добавить строку в целое число, или вызвать len() для объекта, в котором не определена длина.

Ошибка возникает, когда операция или функция применяется к объекту неподходящего типа.

Рассмотрим несколько примеров того, когда возникает TypeError:

>>> 1 + ‘1’

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

>>> ‘1’ + 1

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

TypeError: must be str, not int

>>> len(1)

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

TypeError: object of type ‘int’ has no len()

Указанные выше примеры возникновения TypeError приводят к строке уведомления об ошибке с разными сообщениями. Каждое из них весьма точно информирует вас о том, что пошло не так.

В первых двух примерах мы пытаемся внести строки и целые числа вместе. Однако, они немного отличаются:

  • В первом примере мы пытаемся добавить str к int.
  • Во втором примере мы пытаемся добавить int к str.

Уведомления об ошибке указывают на эти различия.

Последний пример пытается вызвать len() для int. Сообщение об ошибке говорит нам, что мы не можем сделать это с int.

Возникла ошибка ValueError в Python 3 [Решено]

ValueError возникает тогда, когда значение объекта не является корректным. Мы можем рассматривать это как IndexError, которая возникает из-за того, что значение индекса находится вне рамок последовательности, только ValueError является более обобщенным случаем.

Возникает, когда операция или функция получает аргумент, который имеет правильный тип, но неправильное значение, и ситуация не описывается более детальной ошибкой, такой как IndexError.

Вот два примера возникновения ошибки ValueError:

>>> a, b, c = [1, 2]

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

ValueError: not enough values to unpack (expected 3, got 2)

>>> a, b = [1, 2, 3]

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

ValueError: too many values to unpack (expected 2)

Строка уведомления об ошибке ValueError в данных примерах говорит нам в точности, в чем заключается проблема со значениями:

  1. В первом примере, мы пытаемся распаковать слишком много значений. Строка уведомления об ошибке даже говорит нам, где именно ожидается распаковка трех значений, но получаются только два.
  2. Во втором примере, проблема в том, что мы получаем слишком много значений, при этом получаем недостаточно значений для распаковки.

Логирование ошибок из Traceback в Python 3

Получение ошибки, и ее итоговой трассировки указывает на то, что вам нужно предпринять для решения проблемы. Обычно, отладка кода — это первый шаг, но иногда проблема заключается в неожиданном, или некорректном вводе. Хотя важно предусматривать такие ситуации, иногда есть смысл скрывать или игнорировать ошибку путем логирования traceback.

Рассмотрим жизненный пример кода, в котором нужно заглушить трассировки Python. В этом примере используется библиотека requests.

Файл urlcaller.py:

import sys

import requests

response = requests.get(sys.argv[1])

print(response.status_code, response.content)

Этот код работает исправно. Когда вы запускаете этот скрипт, задавая ему URL в качестве аргумента командной строки, он откроет данный URL, и затем выведет HTTP статус кода и содержимое страницы (content) из response. Это работает даже в случае, если ответом является статус ошибки HTTP:

$ python urlcaller.py https://httpbin.org/status/200

200 b»

$ python urlcaller.py https://httpbin.org/status/500

500 b»

Однако, иногда данный URL не существует (ошибка 404 — страница не найдена), или сервер не работает. В таких случаях, этот скрипт приводит к ошибке ConnectionError и выводит трассировку:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

$ python urlcaller.py http://thisurlprobablydoesntexist.com

...

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File «urlcaller.py», line 5, in <module>

    response = requests.get(sys.argv[1])

  File «/path/to/requests/api.py», line 75, in get

    return request(‘get’, url, params=params, **kwargs)

  File «/path/to/requests/api.py», line 60, in request

    return session.request(method=method, url=url, **kwargs)

  File «/path/to/requests/sessions.py», line 533, in request

    resp = self.send(prep, **send_kwargs)

  File «/path/to/requests/sessions.py», line 646, in send

    r = adapter.send(request, **kwargs)

  File «/path/to/requests/adapters.py», line 516, in send

    raise ConnectionError(e, request=request)

requests.exceptions.ConnectionError: HTTPConnectionPool(host=‘thisurlprobablydoesntexist.com’, port=80): Max retries exceeded with url: / (Caused by NewConnectionError(‘<urllib3.connection.HTTPConnection object at 0x7faf9d671860>: Failed to establish a new connection: [Errno -2] Name or service not known’,))

Трассировка Python в данном случае может быть очень длинной, и включать в себя множество других ошибок, которые в итоге приводят к ошибке ConnectionError. Если вы перейдете к трассировке последних ошибок, вы заметите, что все проблемы в коде начались на пятой строке файла urlcaller.py.

Если вы обернёте неправильную строку в блоке try и except, вы сможете найти нужную ошибку, которая позволит вашему скрипту работать с большим числом вводов:

Файл urlcaller.py:

try:

    response = requests.get(sys.argv[1])

except requests.exceptions.ConnectionError:

    print(1, ‘Connection Error’)

else:

    print(response.status_code, response.content)

Код выше использует предложение else с блоком except.

Теперь, когда вы запускаете скрипт на URL, который приводит к ошибке ConnectionError, вы получите -1 в статусе кода и содержимое ошибки подключения:

$ python urlcaller.py http://thisurlprobablydoesntexist.com

1 Connection Error

Это работает отлично. Однако, в более реалистичных системах, вам не захочется просто игнорировать ошибку и итоговую трассировку, вам скорее понадобиться внести в журнал. Ведение журнала трассировок позволит вам лучше понять, что идет не так в ваших программах.

Обратите внимание: Для более лучшего представления о системе логирования в Python вы можете ознакомиться с данным руководством тут: Логирование в Python

Вы можете вести журнал трассировки в скрипте, импортировав пакет logging, получить logger, вызвать .exception() для этого логгера в куске except блока try и except. Конечный скрипт будет выглядеть примерно так:

# urlcaller.py

import logging

import sys

import requests

logger = logging.getLogger(__name__)

try:

    response = requests.get(sys.argv[1])

except requests.exceptions.ConnectionError as e:

    logger.exception()

    print(1, ‘Connection Error’)

else:

    print(response.status_code, response.content)

Теперь, когда вы запускаете скрипт с проблемным URL, он будет выводить исключенные -1 и ConnectionError, но также будет вести журнал трассировки:

$ python urlcaller.py http://thisurlprobablydoesntexist.com

...

  File «/path/to/requests/adapters.py», line 516, in send

    raise ConnectionError(e, request=request)

requests.exceptions.ConnectionError: HTTPConnectionPool(host=‘thisurlprobablydoesntexist.com’, port=80): Max retries exceeded with url: / (Caused by NewConnectionError(‘<urllib3.connection.HTTPConnection object at 0x7faf9d671860>: Failed to establish a new connection: [Errno -2] Name or service not known’,))

1 Connection Error

По умолчанию, Python будет выводить ошибки в стандартный stderr. Выглядит так, будто мы совсем не подавили вывод трассировки. Однако, если вы выполните еще один вызов при перенаправлении stderr, вы увидите, что система ведения журналов работает, и мы можем изучать логи программы без необходимости личного присутствия во время появления ошибок:

$ python urlcaller.py http://thisurlprobablydoesntexist.com 2> mylogs.log

1 Connection Error

Подведем итоги данного обучающего материала

Трассировка Python содержит замечательную информацию, которая может помочь вам понять, что идет не так с вашим кодом Python. Эти трассировки могут выглядеть немного запутанно, но как только вы поймете что к чему, и увидите, что они в себе несут, они могут быть предельно полезными. Изучив несколько трассировок, строку за строкой, вы получите лучшее представление о предоставляемой информации.

Понимание содержимого трассировки Python, когда вы запускаете ваш код может быть ключом к улучшению вашего кода. Это способ, которым Python пытается вам помочь.

Теперь, когда вы знаете как читать трассировку Python, вы можете выиграть от изучения ряда инструментов и техник для диагностики проблемы, о которой вам сообщает трассировка. Модуль traceback может быть полезным, если вам нужно узнать больше из выдачи трассировки.

  • Текст является переводом статьи: Understanding the Python Traceback
  • Изображение из шапки статьи принадлежит сайту © Real Python

Являюсь администратором нескольких порталов по обучению языков программирования Python, Golang и Kotlin. В составе небольшой команды единомышленников, мы занимаемся популяризацией языков программирования на русскоязычную аудиторию. Большая часть статей была адаптирована нами на русский язык и распространяется бесплатно.

E-mail: vasile.buldumac@ati.utm.md

Образование
Universitatea Tehnică a Moldovei (utm.md)

  • 2014 — 2018 Технический Университет Молдовы, ИТ-Инженер. Тема дипломной работы «Автоматизация покупки и продажи криптовалюты используя технический анализ»
  • 2018 — 2020 Технический Университет Молдовы, Магистр, Магистерская диссертация «Идентификация человека в киберпространстве по фотографии лица»

Prerequisite: Python Traceback

To print stack trace for an exception the suspicious code will be kept in the try block and except block will be employed to handle the exception generated. Here we will be printing the stack trace to handle the exception generated. The printing stack trace for an exception helps in understanding the error and what went wrong with the code. Not just this, the stack trace also shows where the error occurred.

The general structure of a stack trace for an exception: 

  • Traceback for the most recent call.
  • Location of the program.
  • Line in the program where the error was encountered.
  • Name of the error: relevant information about the exception
     

Example: 

Traceback (most recent call last):
  File "C:/Python27/hdg.py", line 5, in 
    value=A[5]
IndexError: list index out of range

Method 1: By using print_exc() method.

This method prints exception information and stack trace entries from traceback object tb to file.

Syntax: traceback.print_exc(limit=Nonefile=Nonechain=True)

Parameters: This method accepts the following parameters:

  • if a limit argument is positive, Print up to limit stack trace entries from traceback object tb (starting from the caller’s frame). Otherwise, print the last abs(limit) entries. If the limit argument is None, all entries are printed.
  • If the file argument is None, the output goes to sys.stderr; otherwise, it should be an open file or file-like object to receive the output.
  • If chain argument is true (the default), then chained exceptions will be printed as well, like the interpreter itself does when printing an unhandled exception.

Return: None.

Code:

Python3

import traceback

A = [1, 2, 3, 4]

try:

    value = A[5]

except:

    traceback.print_exc()

print("end of program")

Output: 

Traceback (most recent call last):
  File "C:/Python27/hdg.py", line 8, in 
    value=A[5]
IndexError: list index out of range
end of program

Method 2: By using print_exception() method.

This method prints exception information and stack trace entries from traceback object tb to file.

Syntax : traceback.print_exception(etypevaluetblimit=Nonefile=Nonechain=True)

Parameters: This method accepts the following parameters:

  • if tb argument is not None, it prints a header Traceback (most recent call last):
  • it prints the exception etype and value after the stack trace
  • if type(value) argument is SyntaxError and value has the appropriate format, it prints the line where the syntax error occurred with a caret indicating the approximate position of the error.
  • if a limit argument is positive, Print up to limit stack trace entries from traceback object tb (starting from the caller’s frame). Otherwise, print the last abs(limit) entries. If the limit argument is None, all entries are printed.
  • If the file argument is None, the output goes to sys.stderr; otherwise, it should be an open file or file-like object to receive the output.
  • If chain argument is true (the default), then chained exceptions will be printed as well, like the interpreter itself does when printing an unhandled exception.

Return: None.

Code:

Python3

import traceback

import sys

a = 4

b = 0

try:

    value = a / b

except:

    traceback.print_exception(*sys.exc_info())

print("end of program")

Output: 

Traceback (most recent call last):
  File "C:/Python27/hdg.py", line 10, in 
    value=a/b
ZeroDivisionError: integer division or modulo by zero
end of program

Source code: Lib/traceback.py


This module provides a standard interface to extract, format and print stack
traces of Python programs. It exactly mimics the behavior of the Python
interpreter when it prints a stack trace. This is useful when you want to print
stack traces under program control, such as in a “wrapper” around the
interpreter.

The module uses traceback objects — this is the object type that is stored in
the sys.last_traceback variable and returned as the third item from
sys.exc_info().

The module defines the following functions:

traceback.print_tb(tb, limit=None, file=None)

Print up to limit stack trace entries from traceback object tb (starting
from the caller’s frame) if limit is positive. Otherwise, print the last
abs(limit) entries. If limit is omitted or None, all entries are
printed. If file is omitted or None, the output goes to
sys.stderr; otherwise it should be an open file or file-like object to
receive the output.

Changed in version 3.5: Added negative limit support.

traceback.print_exception(etype, value, tb, limit=None, file=None, chain=True)

Print exception information and stack trace entries from traceback object
tb to file. This differs from print_tb() in the following
ways:

  • if tb is not None, it prints a header Traceback (most recent
    call last):
  • it prints the exception etype and value after the stack trace
  • if type(value) is SyntaxError and value has the appropriate
    format, it prints the line where the syntax error occurred with a caret
    indicating the approximate position of the error.

The optional limit argument has the same meaning as for print_tb().
If chain is true (the default), then chained exceptions (the
__cause__ or __context__ attributes of the exception) will be
printed as well, like the interpreter itself does when printing an unhandled
exception.

Changed in version 3.5: The etype argument is ignored and inferred from the type of value.

traceback.print_exc(limit=None, file=None, chain=True)

This is a shorthand for print_exception(*sys.exc_info(), limit, file,
chain)
.

traceback.print_last(limit=None, file=None, chain=True)

This is a shorthand for print_exception(sys.last_type, sys.last_value,
sys.last_traceback, limit, file, chain)
. In general it will work only
after an exception has reached an interactive prompt (see
sys.last_type).

traceback.print_stack(f=None, limit=None, file=None)

Print up to limit stack trace entries (starting from the invocation
point) if limit is positive. Otherwise, print the last abs(limit)
entries. If limit is omitted or None, all entries are printed.
The optional f argument can be used to specify an alternate stack frame
to start. The optional file argument has the same meaning as for
print_tb().

Changed in version 3.5: Added negative limit support.

Return a list of “pre-processed” stack trace entries extracted from the
traceback object tb. It is useful for alternate formatting of
stack traces. The optional limit argument has the same meaning as for
print_tb(). A “pre-processed” stack trace entry is a 4-tuple
(filename, line number, function name, text) representing the
information that is usually printed for a stack trace. The text is a
string with leading and trailing whitespace stripped; if the source is
not available it is None.

Extract the raw traceback from the current stack frame. The return value has
the same format as for extract_tb(). The optional f and limit
arguments have the same meaning as for print_stack().

traceback.format_list(extracted_list)

Given a list of tuples as returned by extract_tb() or
extract_stack(), return a list of strings ready for printing. Each
string in the resulting list corresponds to the item with the same index in
the argument list. Each string ends in a newline; the strings may contain
internal newlines as well, for those items whose source text line is not
None.

traceback.format_exception_only(etype, value)

Format the exception part of a traceback. The arguments are the exception
type and value such as given by sys.last_type and sys.last_value.
The return value is a list of strings, each ending in a newline. Normally,
the list contains a single string; however, for SyntaxError
exceptions, it contains several lines that (when printed) display detailed
information about where the syntax error occurred. The message indicating
which exception occurred is the always last string in the list.

traceback.format_exception(etype, value, tb, limit=None, chain=True)

Format a stack trace and the exception information. The arguments have the
same meaning as the corresponding arguments to print_exception(). The
return value is a list of strings, each ending in a newline and some
containing internal newlines. When these lines are concatenated and printed,
exactly the same text is printed as does print_exception().

Changed in version 3.5: The etype argument is ignored and inferred from the type of value.

traceback.format_exc(limit=None, chain=True)

This is like print_exc(limit) but returns a string instead of printing to
a file.

traceback.format_tb(tb, limit=None)

A shorthand for format_list(extract_tb(tb, limit)).

traceback.format_stack(f=None, limit=None)

A shorthand for format_list(extract_stack(f, limit)).

traceback.clear_frames(tb)

Clears the local variables of all the stack frames in a traceback tb
by calling the clear() method of each frame object.

New in version 3.4.

traceback.walk_stack(f)

Walk a stack following f.f_back from the given frame, yielding the frame
and line number for each frame. If f is None, the current stack is
used. This helper is used with StackSummary.extract().

New in version 3.5.

traceback.walk_tb(tb)

Walk a traceback following tb_next yielding the frame and line number
for each frame. This helper is used with StackSummary.extract().

New in version 3.5.

The module also defines the following classes:

29.9.1. TracebackException Objects¶

New in version 3.5.

TracebackException objects are created from actual exceptions to
capture data for later printing in a lightweight fashion.

class traceback.TracebackException(exc_type, exc_value, exc_traceback, *, limit=None, lookup_lines=True, capture_locals=False)

Capture an exception for later rendering. limit, lookup_lines and
capture_locals are as for the StackSummary class.

Note that when locals are captured, they are also shown in the traceback.

__cause__

A TracebackException of the original __cause__.

__context__

A TracebackException of the original __context__.

__suppress_context__

The __suppress_context__ value from the original exception.

stack

A StackSummary representing the traceback.

exc_type

The class of the original traceback.

filename

For syntax errors — the file name where the error occurred.

lineno

For syntax errors — the line number where the error occurred.

text

For syntax errors — the text where the error occurred.

offset

For syntax errors — the offset into the text where the error occurred.

msg

For syntax errors — the compiler error message.

classmethod from_exception(exc, *, limit=None, lookup_lines=True, capture_locals=False)

Capture an exception for later rendering. limit, lookup_lines and
capture_locals are as for the StackSummary class.

Note that when locals are captured, they are also shown in the traceback.

format(*, chain=True)

Format the exception.

If chain is not True, __cause__ and __context__ will not
be formatted.

The return value is a generator of strings, each ending in a newline and
some containing internal newlines. print_exception()
is a wrapper around this method which just prints the lines to a file.

The message indicating which exception occurred is always the last
string in the output.

format_exception_only()

Format the exception part of the traceback.

The return value is a generator of strings, each ending in a newline.

Normally, the generator emits a single string; however, for
SyntaxError exceptions, it emits several lines that (when
printed) display detailed information about where the syntax
error occurred.

The message indicating which exception occurred is always the last
string in the output.

29.9.2. StackSummary Objects¶

New in version 3.5.

StackSummary objects represent a call stack ready for formatting.

class traceback.StackSummary

Construct a StackSummary object from a frame generator (such as
is returned by walk_stack() or
walk_tb()).

If limit is supplied, only this many frames are taken from frame_gen.
If lookup_lines is False, the returned FrameSummary
objects will not have read their lines in yet, making the cost of
creating the StackSummary cheaper (which may be valuable if it
may not actually get formatted). If capture_locals is True the
local variables in each FrameSummary are captured as object
representations.

classmethod from_list(a_list)

Construct a StackSummary object from a supplied old-style list
of tuples. Each tuple should be a 4-tuple with filename, lineno, name,
line as the elements.

format()

Returns a list of strings ready for printing. Each string in the
resulting list corresponds to a single frame from the stack.
Each string ends in a newline; the strings may contain internal
newlines as well, for those items with source text lines.

For long sequences of the same frame and line, the first few
repetitions are shown, followed by a summary line stating the exact
number of further repetitions.

Changed in version 3.6: Long sequences of repeated frames are now abbreviated.

29.9.3. FrameSummary Objects¶

New in version 3.5.

FrameSummary objects represent a single frame in a traceback.

class traceback.FrameSummary(filename, lineno, name, lookup_line=True, locals=None, line=None)

Represent a single frame in the traceback or stack that is being formatted
or printed. It may optionally have a stringified version of the frames
locals included in it. If lookup_line is False, the source code is not
looked up until the FrameSummary has the line
attribute accessed (which also happens when casting it to a tuple).
line may be directly provided, and will prevent line
lookups happening at all. locals is an optional local variable
dictionary, and if supplied the variable representations are stored in the
summary for later display.

29.9.4. Traceback Examples¶

This simple example implements a basic read-eval-print loop, similar to (but
less useful than) the standard Python interactive interpreter loop. For a more
complete implementation of the interpreter loop, refer to the code
module.

import sys, traceback

def run_user_code(envdir):
    source = input(">>> ")
    try:
        exec(source, envdir)
    except Exception:
        print("Exception in user code:")
        print("-"*60)
        traceback.print_exc(file=sys.stdout)
        print("-"*60)

envdir = {}
while True:
    run_user_code(envdir)

The following example demonstrates the different ways to print and format the
exception and traceback:

import sys, traceback

def lumberjack():
    bright_side_of_death()

def bright_side_of_death():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("*** print_tb:")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print("*** print_exception:")
    # exc_type below is ignored on 3.5 and later
    traceback.print_exception(exc_type, exc_value, exc_traceback,
                              limit=2, file=sys.stdout)
    print("*** print_exc:")
    traceback.print_exc(limit=2, file=sys.stdout)
    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])
    print("*** format_exception:")
    # exc_type below is ignored on 3.5 and later
    print(repr(traceback.format_exception(exc_type, exc_value,
                                          exc_traceback)))
    print("*** extract_tb:")
    print(repr(traceback.extract_tb(exc_traceback)))
    print("*** format_tb:")
    print(repr(traceback.format_tb(exc_traceback)))
    print("*** tb_lineno:", exc_traceback.tb_lineno)

The output for the example would look similar to this:

*** print_tb:
  File "<doctest...>", line 10, in <module>
    lumberjack()
*** print_exception:
Traceback (most recent call last):
  File "<doctest...>", line 10, in <module>
    lumberjack()
  File "<doctest...>", line 4, in lumberjack
    bright_side_of_death()
IndexError: tuple index out of range
*** print_exc:
Traceback (most recent call last):
  File "<doctest...>", line 10, in <module>
    lumberjack()
  File "<doctest...>", line 4, in lumberjack
    bright_side_of_death()
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
*** format_exception:
['Traceback (most recent call last):n',
 '  File "<doctest...>", line 10, in <module>n    lumberjack()n',
 '  File "<doctest...>", line 4, in lumberjackn    bright_side_of_death()n',
 '  File "<doctest...>", line 7, in bright_side_of_deathn    return tuple()[0]n',
 'IndexError: tuple index out of rangen']
*** extract_tb:
[<FrameSummary file <doctest...>, line 10 in <module>>,
 <FrameSummary file <doctest...>, line 4 in lumberjack>,
 <FrameSummary file <doctest...>, line 7 in bright_side_of_death>]
*** format_tb:
['  File "<doctest...>", line 10, in <module>n    lumberjack()n',
 '  File "<doctest...>", line 4, in lumberjackn    bright_side_of_death()n',
 '  File "<doctest...>", line 7, in bright_side_of_deathn    return tuple()[0]n']
*** tb_lineno: 10

The following example shows the different ways to print and format the stack:

>>> import traceback
>>> def another_function():
...     lumberstack()
...
>>> def lumberstack():
...     traceback.print_stack()
...     print(repr(traceback.extract_stack()))
...     print(repr(traceback.format_stack()))
...
>>> another_function()
  File "<doctest>", line 10, in <module>
    another_function()
  File "<doctest>", line 3, in another_function
    lumberstack()
  File "<doctest>", line 6, in lumberstack
    traceback.print_stack()
[('<doctest>', 10, '<module>', 'another_function()'),
 ('<doctest>', 3, 'another_function', 'lumberstack()'),
 ('<doctest>', 7, 'lumberstack', 'print(repr(traceback.extract_stack()))')]
['  File "<doctest>", line 10, in <module>n    another_function()n',
 '  File "<doctest>", line 3, in another_functionn    lumberstack()n',
 '  File "<doctest>", line 8, in lumberstackn    print(repr(traceback.format_stack()))n']

This last example demonstrates the final few formatting functions:

>>> import traceback
>>> traceback.format_list([('spam.py', 3, '<module>', 'spam.eggs()'),
...                        ('eggs.py', 42, 'eggs', 'return "bacon"')])
['  File "spam.py", line 3, in <module>n    spam.eggs()n',
 '  File "eggs.py", line 42, in eggsn    return "bacon"n']
>>> an_error = IndexError('tuple index out of range')
>>> traceback.format_exception_only(type(an_error), an_error)
['IndexError: tuple index out of rangen']
Purpose: Extract, format, and print exceptions and stack traces.
Available In: 1.4 and later, with modifications over time

The traceback module works with the call stack to produce error
messages. A traceback is a stack trace from the point of an exception
handler down the call chain to the point where the exception was
raised. You can also work with the current call stack up from the
point of a call (and without the context of an error), which is useful
for finding out the paths being followed into a function.

The functions in traceback fall into several common categories.
There are functions for extracting raw tracebacks from the current
runtime environment (either an exception handler for a traceback, or
the regular stack). The extracted stack trace is a sequence of tuples
containing the filename, line number, function name, and text of the
source line.

Once extracted, the stack trace can be formatted using functions like
format_exception(), format_stack(), etc. The format
functions return a list of strings with messages formatted to be
printed. There are shorthand functions for printing the formatted
values, as well.

Although the functions in traceback mimic the behavior of the
interactive interpreter by default, they also are useful for handling
exceptions in situations where dumping the full stack trace to stderr
is not desirable. For example, a web application may need to format
the traceback so it looks good in HTML. An IDE may convert the
elements of the stack trace into a clickable list that lets the user
browse the source.

Supporting Functions¶

The examples below use the module traceback_example.py (provided in
the source package for PyMOTW). The contents are:

import traceback
import sys

def produce_exception(recursion_level=2):
    sys.stdout.flush()
    if recursion_level:
        produce_exception(recursion_level-1)
    else:
        raise RuntimeError()

def call_function(f, recursion_level=2):
    if recursion_level:
        return call_function(f, recursion_level-1)
    else:
        return f()

Working With Exceptions¶

The simplest way to handle exception reporting is with
print_exc(). It uses sys.exc_info() to obtain the
exception information for the current thread, formats the results, and
prints the text to a file handle (sys.stderr, by default).

import traceback
import sys

from traceback_example import produce_exception

print 'print_exc() with no exception:'
traceback.print_exc(file=sys.stdout)
print

try:
    produce_exception()
except Exception, err:
    print 'print_exc():'
    traceback.print_exc(file=sys.stdout)
    print
    print 'print_exc(1):'
    traceback.print_exc(limit=1, file=sys.stdout)

In this example, the file handle for sys.stdout is substituted so
the informational and traceback messages are mingled correctly:

$ python traceback_print_exc.py

print_exc() with no exception:
None

print_exc():
Traceback (most recent call last):
  File "traceback_print_exc.py", line 20, in <module>
    produce_exception()
  File "/Users/dhellmann/Documents/PyMOTW/src/PyMOTW/traceback/traceback_example.py", line 16, in produce_exception
    produce_exception(recursion_level-1)
  File "/Users/dhellmann/Documents/PyMOTW/src/PyMOTW/traceback/traceback_example.py", line 16, in produce_exception
    produce_exception(recursion_level-1)
  File "/Users/dhellmann/Documents/PyMOTW/src/PyMOTW/traceback/traceback_example.py", line 18, in produce_exception
    raise RuntimeError()
RuntimeError

print_exc(1):
Traceback (most recent call last):
  File "traceback_print_exc.py", line 20, in <module>
    produce_exception()
RuntimeError

print_exc() is just a shortcut for print_exception(),
which requires explicit arguments:

import traceback
import sys

from traceback_example import produce_exception

try:
    produce_exception()
except Exception, err:
    print 'print_exception():'
    exc_type, exc_value, exc_tb = sys.exc_info()
    traceback.print_exception(exc_type, exc_value, exc_tb)
$ python traceback_print_exception.py

Traceback (most recent call last):
  File "traceback_print_exception.py", line 16, in <module>
    produce_exception()
  File "/Users/dhellmann/Documents/PyMOTW/src/PyMOTW/traceback/traceback_example.py", line 16, in produce_exception
    produce_exception(recursion_level-1)
  File "/Users/dhellmann/Documents/PyMOTW/src/PyMOTW/traceback/traceback_example.py", line 16, in produce_exception
    produce_exception(recursion_level-1)
  File "/Users/dhellmann/Documents/PyMOTW/src/PyMOTW/traceback/traceback_example.py", line 18, in produce_exception
    raise RuntimeError()
RuntimeError
print_exception():

And print_exception() uses format_exception():

import traceback
import sys
from pprint import pprint

from traceback_example import produce_exception

try:
    produce_exception()
except Exception, err:
    print 'format_exception():'
    exc_type, exc_value, exc_tb = sys.exc_info()
    pprint(traceback.format_exception(exc_type, exc_value, exc_tb))
$ python traceback_format_exception.py

format_exception():
['Traceback (most recent call last):n',
 '  File "traceback_format_exception.py", line 17, in <module>n    produce_exception()n',
 '  File "/Users/dhellmann/Documents/PyMOTW/src/PyMOTW/traceback/traceback_example.py", line 16, in produce_exceptionn    produce_exception(recursion_level-1)n',
 '  File "/Users/dhellmann/Documents/PyMOTW/src/PyMOTW/traceback/traceback_example.py", line 16, in produce_exceptionn    produce_exception(recursion_level-1)n',
 '  File "/Users/dhellmann/Documents/PyMOTW/src/PyMOTW/traceback/traceback_example.py", line 18, in produce_exceptionn    raise RuntimeError()n',
 'RuntimeErrorn']

Working With the Stack¶

There are a similar set of functions for performing the same operations with
the current call stack instead of a traceback.

print_stack()¶

import traceback
import sys

from traceback_example import call_function

def f():
    traceback.print_stack(file=sys.stdout)

print 'Calling f() directly:'
f()

print
print 'Calling f() from 3 levels deep:'
call_function(f)
$ python traceback_print_stack.py

Calling f() directly:
  File "traceback_print_stack.py", line 19, in <module>
    f()
  File "traceback_print_stack.py", line 16, in f
    traceback.print_stack(file=sys.stdout)

Calling f() from 3 levels deep:
  File "traceback_print_stack.py", line 23, in <module>
    call_function(f)
  File "/Users/dhellmann/Documents/PyMOTW/src/PyMOTW/traceback/traceback_example.py", line 22, in call_function
    return call_function(f, recursion_level-1)
  File "/Users/dhellmann/Documents/PyMOTW/src/PyMOTW/traceback/traceback_example.py", line 22, in call_function
    return call_function(f, recursion_level-1)
  File "/Users/dhellmann/Documents/PyMOTW/src/PyMOTW/traceback/traceback_example.py", line 24, in call_function
    return f()
  File "traceback_print_stack.py", line 16, in f
    traceback.print_stack(file=sys.stdout)

format_stack()¶

import traceback
import sys
from pprint import pprint

from traceback_example import call_function

def f():
    return traceback.format_stack()

formatted_stack = call_function(f)
pprint(formatted_stack)
$ python traceback_format_stack.py

['  File "traceback_format_stack.py", line 19, in <module>n    formatted_stack = call_function(f)n',
 '  File "/Users/dhellmann/Documents/PyMOTW/src/PyMOTW/traceback/traceback_example.py", line 22, in call_functionn    return call_function(f, recursion_level-1)n',
 '  File "/Users/dhellmann/Documents/PyMOTW/src/PyMOTW/traceback/traceback_example.py", line 22, in call_functionn    return call_function(f, recursion_level-1)n',
 '  File "/Users/dhellmann/Documents/PyMOTW/src/PyMOTW/traceback/traceback_example.py", line 24, in call_functionn    return f()n',
 '  File "traceback_format_stack.py", line 17, in fn    return traceback.format_stack()n']

Понравилась статья? Поделить с друзьями:
  • Primary hdd bay ошибка
  • Primary filegroup is full как исправить
  • Primary battery 601 hp ошибка как отключить
  • Primary battery 601 hp ошибка как исправить
  • Pri master hdd error