Fix IndentationError in Python
Error Reference
IndentationError
Mixed tabs/spaces or missing blocks trigger IndentationError. Fix them quickly.
IndentationError comes in three distinct messages, and each points at a different mistake. Match the message you got to the snippet below.
Expected an indented block
Every line ending in a colon promises a body. Leave it flush-left and Python complains at the next line:
def greet(name):
print(f'Hello, {name}')
File "greet.py", line 2
print(f'Hello, {name}')
^
IndentationError: expected an indented block after function definition on line 1
Indent the body, or use pass as an explicit placeholder for a body you'll write later:
def greet(name):
print(f'Hello, {name}')
def todo():
pass # valid empty body
Unexpected indent
The opposite problem — a line is indented where no block was opened:
print('first')
print('second')
File "steps.py", line 2
print('second')
IndentationError: unexpected indent
Common after deleting an if or for line but leaving its body indented. Dedent the stray lines back to the surrounding level.
Unindent does not match
A line dedents to a level that never existed. Here print('b') uses 2 spaces, but the only levels in play are 0 and 4:
if True:
print('a')
print('b')
File "branch.py", line 3
print('b')
IndentationError: unindent does not match any outer indentation level
Every line in a block must start at exactly the same column. Pick one — PEP 8 says 4 spaces — and make the whole block agree.
Tabs vs spaces
Mixing tabs and spaces in one block raises TabError, a subclass of IndentationError. The lines may look aligned in your editor while Python sees different indentation:
def report():
print('indented with a tab')
print('indented with eight spaces')
File "report.py", line 3
print('indented with eight spaces')
TabError: inconsistent use of tabs and spaces in indentation
Fix it at the editor level, once:
- Set "insert spaces when pressing Tab" (VS Code:
"editor.insertSpaces": true), 4 per indent. - Turn on whitespace rendering so tabs and spaces are visibly different.
- Convert the offending file with your editor's "Convert indentation to spaces" command.
Catch it before Python does
Tools spot inconsistent whitespace faster than reading tracebacks:
python -m tabnanny script.py # reports ambiguous tab/space mixes
black script.py # reformats to a consistent 4-space style
Linters (flake8, ruff) flag tab indentation and bad continuation lines as you type, and an .editorconfig file keeps every collaborator's editor on the same settings.
Frequently Asked Questions
What is the difference between TabError and IndentationError?
TabError is a subclass of IndentationError raised for one specific cause: tabs and spaces mixed inconsistently in the same block. Generic IndentationError covers missing or misaligned blocks. You almost never catch either in code — they fire at parse time, so the fix is editing the file, not exception handling.
Why does code I copy-pasted from a website fail?
Web pages, PDFs, and chat apps often convert leading whitespace — tabs become spaces, non-breaking spaces sneak in, or indentation is stripped entirely. The code looks right but the invisible characters differ. Re-indent the pasted block in your editor, or run it through black to normalize it.
Is indentation really part of Python syntax?
Yes. Python has no braces; a block is defined entirely by its indentation, and the end of a block is a return to a shallower level. That makes whitespace as meaningful as any keyword — which is exactly why the parser refuses to guess when levels are ambiguous.
if True:
print('inside the if block')
print('after the if block')