English
/

How to Read a Python Error Message

Last edited Oct 30, 2026 · 4 min read

Read a Python traceback from the bottom: identify the exception type and message, then inspect the failing source line and the calls above it. Syntax errors instead show where Python could not parse a statement, which may be just after the actual mistake.

On this page▾
  1. The two kinds of errors
  2. Reading a traceback from the bottom up
  3. Handling errors instead of crashing
  4. Match the error to a next step
  5. Frequently asked questions
  6. Does the caret always mark the code I must change?
  7. Should I use except Exception to hide every error?
  8. Why do I see two tracebacks in one message?

The two kinds of errors

The official Python tutorial separates errors into two categories. Syntax errors happen when Python can't even understand your code's structure, for example a missing colon:

>>> while True print('Hello world')
  File "<stdin>", line 1
    while True print('Hello world')
               ^^^^^
SyntaxError: invalid syntax

The little arrow (^^^^^) points at roughly where Python got confused, though the tutorial notes this isn't always the exact spot that needs fixing. In this example, the real issue is the missing colon right before print.

Exceptions happen when your code is structurally valid Python but something goes wrong while it's actually running. The distinction tells you whether to inspect the statement structure or the values used during execution.

Reading a traceback from the bottom up

To read Python errors, read the last line first, then work upward only if you need more context. In an ordinary traceback without added notes, the final exception line has two parts: the exception type, and a message describing what happened.

>>> 10 * (1/0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    10 * (1/0)
ZeroDivisionError: division by zero

ZeroDivisionError: division by zero tells you immediately what happened: you divided by zero. The lines above it (the "traceback") show you the path the code took to get there, which matters more once your program has multiple functions calling each other, but for a single line like this, the last line already has your answer.

Here are four useful beginner examples:

>>> n
NameError: name 'n' is not defined

You tried to use a variable that was never created, often because of a typo in the name or because you ran a later cell/line before the one that defines it.

>>> '2' + 2
TypeError: can only concatenate str (not "int") to str

You tried to combine two different, incompatible types. Here, Python will not automatically decide whether '2' + 2 should become '22' or 4, so it raises an error and makes you choose with an explicit conversion.

>>> 10 * (1/0)
ZeroDivisionError: division by zero

Exactly what it says: a division where the bottom number was zero.

>>> word = "Python"
>>> word[42]
IndexError: string index out of range

You asked for a position in a string (or list) that doesn't exist. "Python" only has 6 characters (positions 0 through 5), so asking for position 42 fails.

Handling errors instead of crashing

Once you understand what an error means, you can decide whether your program should catch it and keep running, using try and except. The official tutorial's example for asking a user for a number demonstrates the pattern:

while True:
    try:
        x = int(input("Please enter a number: "))
        break
    except ValueError:
        print("Oops!  That was no valid number.  Try again...")

The try block runs first. If it raises a ValueError (which happens if the user types something that isn't a number), the except block catches it and the loop asks again instead of crashing the whole program.

You can also add an else clause, which runs only if no exception occurred, and a finally clause, which always runs no matter what. Running this tested example shows the difference clearly:

def divide(x, y):
    try:
        result = x / y
    except ZeroDivisionError:
        print("division by zero!")
    else:
        print("result is", result)
    finally:
        print("executing finally clause")

divide(2, 1)
divide(2, 0)

The output is:

result is 2.0
executing finally clause
division by zero!
executing finally clause

divide(2, 1) succeeds, so the else block runs ("result is 2.0"), and finally always runs after it. divide(2, 0) triggers the except block instead ("division by zero!"), and finally still runs regardless of which path was taken.

The file errors you will meet most often are covered in How to Read and Write Text Files in Python.

Match the error to a next step

Exception What to inspect first Example correction
NameError Whether the name was assigned and spelled consistently Define n before using it
TypeError The types involved in the operation Use int('2') + 2 for numeric addition
ValueError Whether the value fits the requested conversion Replace int('two') with valid numeric input
IndexError The index and the sequence length Use word[5], not word[42], for "Python"

The distinction between TypeError and ValueError is useful: int('two') receives a string, which the conversion accepts as an input type, but its contents do not represent an integer. Catching TypeError would not handle that failure.

Frequently asked questions

Does the caret always mark the code I must change?

No. A syntax-error marker shows where parsing failed. A missing colon or an earlier unclosed expression can make the interpreter complain at a later position. Read the surrounding statement too.

Should I use except Exception to hide every error?

No. Catch expected failures specifically. A broad handler can conceal a programming mistake and let execution continue with invalid assumptions.

Why do I see two tracebacks in one message?

An exception can arise while another exception is being handled, or code can explicitly chain an exception with raise ... from .... Read the final exception, then the earlier cause and connecting text rather than treating the whole output as one list of function calls.

Sources

  1. Python Software Foundation, The Python Tutorial, Errors and Exceptions
  2. Python Software Foundation, Built-in Exceptions
  3. Python Software Foundation, traceback module

About this guide

Howlane operator · Checked against the official sources listed above. It is general information, not professional advice.

AboutEditorial standardsCorrectionsReport an error

Edit history

  • Updated
  • Updated
  • Updated
  • Published