Most students searching for how to debug Python code have a script that ran fine an hour ago and now ends in a block of red text, and the quickest route out is to treat that text as a set of directions written by Python for you. The method below uses the official Python tutorial and the documentation for pdb, the debugger that ships with Python, and it works through one small bug from error message to fix.

Read the last line of the traceback first

Take this short program, which is meant to add up a list of prices that the user types on a single line:

def total_price(prices):
    total = 0
    for price in prices:
        total = total + price
    return total


items = input("Prices separated by spaces: ").split()
print(total_price(items))

Typing 2.50 4 makes it stop with a traceback ending in TypeError: unsupported operand type(s) for +: 'int' and 'str'. The Python tutorial explains that the last line of an error message indicates what happened, with the exception type, such as TypeError or NameError, printed as part of it (Python Software Foundation, n.d.-a). Here the message says Python was asked to add a number to a piece of text, which is already most of the diagnosis.

Follow the traceback to the line that failed

The lines above the error form a stack traceback, which shows the context where the exception occurred and lists the source lines involved (Python Software Foundation, n.d.-a). Reading it from the bottom up, the failure is on line 4 inside total_price, which was called from line 9, so the problem is the value of price arriving from the list.

Syntax errors behave a little differently, because the parser repeats the offending line and points arrows at the place where it detected the problem, which is not always the place that needs fixing (Python Software Foundation, n.d.-a). A missing colon at the end of a while or if line is a common case, where the arrows land on the next part of the statement.

How to debug Python code with print statements

A temporary print just before the failing line shows what the variables hold, so adding print(repr(price), type(price)) inside the loop prints '2.50' <class 'str'>. That confirms the cause, because input() returns a string and split() breaks it into more strings, so each price is text and needs converting with float() before it can be added. Remove the extra prints once the bug is found, since a marker reading your code will not expect them.

Step through the code with breakpoint()

For bugs that a single print does not explain, the built-in breakpoint() pauses the program at the line where you place it and opens the debugger, whose prompt is (Pdb) (Python Software Foundation, n.d.-b). At that prompt, p price evaluates an expression and prints its value, n runs to the next line in the current function, s steps into a function being called, and continue lets the program carry on without the debugger.

Stepping line by line is especially useful for loops, because you can watch total change on each pass and see the exact iteration where it stops behaving as expected. The breakpoint() function has been available since Python 3.7 as a shorter alternative to import pdb; pdb.set_trace() (Python Software Foundation, n.d.-b).

Fix one problem and run the program again

Change line 4 to total = total + float(price) and run the script again with the same input, which now prints 6.5. Making one change at a time and re-running with the input that caused the failure tells you whether that change fixed the problem, while several edits at once can hide which one mattered or introduce a new bug.

If you are still setting up your first files, the guides on installing Python and running a program and writing a Python script for a class assignment cover the basics this guide assumes. When an error still makes no sense after these steps, coding support and one-to-one tutoring can walk through the traceback with you.

Frequently asked questions

Why does the error point to a line that looks correct?

For syntax errors, the arrows mark where the parser detected the problem, which is sometimes after the actual mistake, so check the end of the line above as well (Python Software Foundation, n.d.-a).

Is print debugging bad practice?

A few temporary print calls are a quick and common way to check values, and the debugger becomes more useful as programs grow, though the prints should come out before you submit.

What should I include when asking for help with an error?

Copy the full traceback, the code it points to and the input that caused it, since the traceback shows both what happened and where (Python Software Foundation, n.d.-a).

References

Python Software Foundation. (n.d.-a). Errors and exceptions. Python documentation.

Python Software Foundation. (n.d.-b). pdb: The Python debugger. Python documentation.