Python errors › IndentationError
IndentationError: expected an indented block
Updated
The Python error "expected an indented block" means a line ending in a colon has no indented body under it. How to read the message, and the fix.
What you see
def greet(name):
if name:
print("Hello,", name)
greet("Ada") File "/home/me/project/main.py", line 3
print("Hello,", name)
^^^^^
IndentationError: expected an indented block after 'if' statement on line 2What it means
A line that ends in a colon opens a block: if, elif, else, for, while, def, class, with, try, except, finally and match. The next line must be indented further than the line with the colon. Here print sits at the same depth as if, so the if has no body.
From Python 3.10 the message names the statement and the line it is on, which points straight at the block that is empty.
Why it happens
- The body was not indented, as in the example.
- A block was left empty on purpose, as a placeholder:
def todo():with nothing under it. - The body is only a comment. Comments are ignored, so
if ready:followed by# lateris anifwith no body. - Pasted code lost its leading spaces, which happens with chat apps and some web pages.
How to fix it
Indent the body one level, four spaces by convention:
def greet(name):
if name:
print("Hello,", name)
For a block you mean to leave empty, write pass. ... also works:
def todo():
pass
How each Python version words it
| Python | Prints |
|---|---|
| 3.9 | IndentationError: expected an indented block |
| 3.10–3.15 | IndentationError: expected an indented block after 'if' statement on line 2 |
Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on October 25, 2026.
Up to 3.9 the message was only "expected an indented block". From 3.10 it adds what kind of block and its line number.
Questions
Why does a comment not count as a body?
The tokenizer drops comments before the parser sees the code. To the parser, a block containing only a comment is empty.
Tabs or spaces?
Spaces, four per level, as PEP 8 recommends. Mixing the two in one block gives a TabError.
Sources
- Lexical analysis: indentation, Python language reference
- The pass statement, Python language reference
- What's New in Python 3.10: IndentationErrors, Python documentation
Read on October 25, 2026.