Readable code

IndentationError and TabError in Python: Causes and Fixes

Readable code · · Published 2026-12-03

Ink drawing of a young woman in a long dress taking a wide, unsteady step with her arms flung out for balance
Francisco de Goya, Be Careful with that Step!, 1816-1820, brush and ink. Art Institute of Chicago, public domain (CC0).

In most languages, indentation is a matter of taste. In Python it is grammar: the leading spaces of a line tell the interpreter which block the line belongs to. When those spaces do not add up, Python stops before running anything and prints an IndentationError, or its stricter cousin TabError. There are only four messages to learn, and each one points to a precise mistake.

How Python reads indentation

Python does not see blocks the way you do. Before parsing, its tokenizer measures the whitespace at the start of each line and compares it with the line above. The language reference describes the rule exactly: tabs are replaced by one to eight spaces so that the total up to that point is a multiple of eight, and the number of spaces before the first visible character gives the line's indentation level. A deeper level opens a block; a shallower one must return to a level that was used before. The same page adds that indentation is rejected as inconsistent when a file mixes tabs and spaces in a way that makes the meaning depend on the worth of a tab, and that a TabError is raised in that case.

The exceptions documentation places both errors in the family tree: IndentationError is the base class for syntax errors related to incorrect indentation and a subclass of SyntaxError, and TabError is a subclass of IndentationError. That is why a single bad indent stops the whole file, as with any other parsing error; our page on what a syntax error is explains the difference with errors raised while the program runs.

All the output below was produced with CPython 3.13 on 3 December 2026. Older versions print shorter messages for the same mistakes.

1. "expected an indented block after …"

Every line that ends with a colon (def, class, if, for, while, with, try and the rest) announces a block, and the block must contain at least one indented statement.

def greet(name):
print("Hello", name)
  File "a.py", line 2
    print("Hello", name)
    ^^^^^
IndentationError: expected an indented block after function definition on line 1

Recent versions name the statement that opened the block and its line, which makes the fix obvious: indent the body by four spaces. Two variants catch people out. A block that contains only a comment is still empty, because comments are not statements:

for i in range(3):
# todo: process i
  File "g.py", line 2
    # todo: process i
IndentationError: expected an indented block after 'for' statement on line 1

And a function you have not written yet needs a placeholder. Use pass, or the ellipsis ..., which many style guides prefer for stubs:

def load_config(path):
    ...  # to be written

2. "unexpected indent"

The opposite mistake: a line is indented further than the block it belongs to, with no colon above to justify it.

def greet(name):
    print("Hello")
        print(name)
  File "b.py", line 3
    print(name)
IndentationError: unexpected indent

The usual causes are a line pasted from somewhere else with its original indentation, a leftover space at the start of a line in the interactive interpreter, or an if line you deleted while keeping its body. Bring the line back to the level of its neighbours, or restore the statement that opened the block.

3. "unindent does not match any outer indentation level"

This one is the most puzzling to read, and the most precise once you know the rule. When a line is indented less than the line above, Python closes blocks until it finds a level that was used before. If the new level matches none of them, it gives up.

def greet(name):
    if name:
        print(name)
      print("done")
  File "c.py", line 4
    print("done")
                 ^
IndentationError: unindent does not match any outer indentation level

Here the open levels are 0, 4 and 8 spaces, and the last line uses 6. The fix is to choose: 4 spaces if print("done") belongs after the if, 8 if it belongs inside it. The same message appears when an else or except does not line up with its if or try. If the columns look aligned on screen and the error persists, a tab is hiding somewhere: see the next section.

4. TabError: inconsistent use of tabs and spaces

A tab and four spaces can look identical in an editor and mean different things to Python.

def f():
	x = 1         # tab
        return x  # eight spaces
  File "e.py", line 3
    return x
TabError: inconsistent use of tabs and spaces in indentation

Python 2 guessed what you meant; Python 3 refuses, because the meaning would depend on the width of a tab. To find the offending lines, the standard library ships a small checker, tabnanny, which reports ambiguous indentation:

$ python3 -m tabnanny e.py
e.py 3 '        return x'

The lasting fix is a setting, not a search. PEP 8 calls spaces the preferred indentation method and keeps tabs only to remain consistent with code that is already indented with tabs. Configure your editor to insert spaces when you press the Tab key, turn on "show whitespace" while you debug, and convert an existing file once, all in one commit. Our guide to indentation and line length covers the four-space rule and continuation lines in detail.

Catching indentation errors before you run

Sources