liveBook · Manning

Stack trace

As explained in Python How-To

A traceback is a diagnostic report showing the sequence of active calls and execution context when an exception occurs.

A method call fails, but is the line that called it actually wrong? In Python, a stack trace—usually called a traceback—shows the calls that led to an exception. Read it to find where execution failed before deciding what to fix. section 13.1 section 13.1.1

Listing 13.1 Showing a traceback when running some code
class Task:
    def __init__(self, title, urgency):
        self.title = title
        self.urgency = urgency
 
    def _update_db(self):
        # update the record in the database
        print("update the database")
       
    def update_urgency(self, urgency):
        self.urgency = urgency
        self.update_db()        #1
 
 
task = Task("Laundry", 3)
task.update_urgency(4)          #2
# output the following error:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 10, in update_urgency
AttributeError: 'Task' object has no attribute 'update_db'

Run this example to see two calls in the traceback: the call to `update_urgency` and the failing call inside that method. section 13.1

Read the traceback from top to bottom. Its first call shows what started the failing sequence; its last call points to the operation that directly raised the exception. The final line gives the exception type and description. Here, `AttributeError` says that `Task` has no `update_db` attribute. The method is actually named `_update_db`, so change the call inside `update_urgency` and run the code again. section 13.1.2 section 13.1.4

When using a traceback to locate a bug:

  • Start at the bottom: inspect the exception message and the last call’s method name and line number. section 13.1.4
  • If the last call belongs to a dependency, read upward until you reach a call in your own code. section 13.1.5
  • Check where the code ran. Console tracebacks identify input as `<stdin>`; a script traceback identifies the file and shows the operation at the reported line. Console line numbers can differ because submitted empty lines are removed. section 13.1.2 section 13.1.3

A traceback is a snapshot taken after execution stops. If you need to inspect each operation as it happens, use a debugger instead. For practice, add methods that call one another and put a failing operation in the last method; the resulting traceback will show a longer sequence of calls. section 13.2.5 section 13.1.6

How Python How-To covers it

How deep it goes
Explains call order, exception details, and console-versus-script output, but leaves step-by-step execution inspection to the debugger.
Examples
Uses a misspelled method call in a Python class, then compares its console and script tracebacks.
What it assumes
Assumes familiarity with Python classes, methods, and exception types; teaches how to read beyond an exception’s final line.
What you can do after
Diagnose/debug with it by inspecting the last call, then tracing upward when that call belongs to a dependency.
How it connects
Connects tracebacks to exceptions, the call stack, Python scripts, and interactive debugging.

Also explained in

Compare how 7 books explain stack trace →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.