A traceback is not a judgement on your ability; it is a map with the destination printed at the bottom. The final line names the error and the lines above it name the path the interpreter took, from the outermost call to the exact line that failed. Reading Python errors in that order — last line first, then the line number — turns most failures into a two-minute fix.
IndentationError and the invisible tab
Python uses indentation as syntax, so a tab where a space belongs is a real error rather than a cosmetic one. The message usually points at a line that looks perfectly fine, which is why the fix is rarely to retype it. Turn on visible whitespace in your editor, and configure it to insert four spaces for every tab.
If the error appears after pasting code from a web page, the pasted block has mixed whitespace. Select the block and reformat it rather than hunting for the one bad character. Python 3 will not let you mix tabs and spaces in the same block, so the confusion is at least consistent.
NameError, ImportError, and shadowed names
NameError means a name was used before it existed. The usual causes are an assignment that never ran because it sits inside a branch, a variable defined in one function and read in another, and a simple spelling difference between total and totals. Read the reported line and trace backwards: where should this name have been created?
ImportError and ModuleNotFoundError are usually environment problems rather than code problems. Confirm the package is installed in the interpreter the program is actually running under, which is not always the one in your editor’s status bar. A local file named after a library — random.py, email.py, types.py — will shadow the real module and produce genuinely confusing failures.
Silent bugs: mutable defaults and copies
The nastiest mistakes do not raise anything. A mutable default argument is the classic: def collect(item, bucket=[]) creates the list once, at definition time, so every call shares it and the second call behaves differently from the first. Use None as the default and build the container inside the function.
The same family of bug appears with copies. b = a gives two names for one object, so editing b changes a. Use a.copy() for a shallow copy of a list or dictionary, and copy.deepcopy when the container holds nested containers. When in doubt, print id() for both names.
Off-by-one errors and None in the wrong place
Indices start at zero, so the last item of a list of length n is at n - 1. Slicing, by contrast, excludes the end, which makes items[1:3] return two elements and not three. When a loop skips the final record or crashes on the last one, range(len(items)) is usually the culprit — iterate over the items instead, or use enumerate when you need the index.
None tends to arrive from functions that forget to return. A function that prints its result returns None, so assigning that call to a variable and then using it raises TypeError: NoneType is not subscriptable. Check whether the function you called actually returns what you assumed.
A loop that shortens the search
- Read the last line of the traceback, then the first frame in your own code.
- Print the values involved immediately before the failing line.
- Reduce to the smallest script that still fails.
- Fix it, then add a test or a check that would have caught it.
Most debugging time is spent guessing. Printing two variables is almost always faster than reasoning about them.