Article
IndentationError in Python: what it means and how to fix it
Python uses spaces to decide which lines belong together, so indentation is part of the grammar. Here is how to read each IndentationError message and fix it fast.
On this page0%
- The four messages, decoded
- Step 1 — Read the arrow, not the whole file
- Step 2 — Fix "expected an indented block"
- Step 3 — Fix "unexpected indent"
- Step 4 — Fix "unindent does not match any outer indentation level"
- Step 5 — Kill tabs before they kill your afternoon
- Step 6 — Check the line above when nothing looks wrong
- A quick self-check routine
- What you learned
- FAQ
- What causes IndentationError in Python?
- How do I fix "expected an indented block"?
- Should I use tabs or spaces in Python?
- Why does my code look correctly indented but still error?
- How do I fix indentation across a whole project at once?
- Next reading on Sythra Articles
In most languages, spaces are decoration. In Python, spaces are grammar. Python uses indentation to decide which lines belong inside an if, a loop, or a function. When the spacing does not match, you get an IndentationError — and your program stops before running a single line.
The good news: there are only four versions of this error, and each one tells you exactly what happened.
The four messages, decoded
| Message | Plain English | Usual cause |
|---|---|---|
expected an indented block | You opened a block and left it empty | Missing indented line after if, for, def |
unexpected indent | This line is pushed in for no reason | Stray spaces at the start of a line |
unindent does not match any outer indentation level | Your closing level matches nothing above | Mixed 2-space and 4-space indents |
TabError: inconsistent use of tabs and spaces | Some lines use tabs, others use spaces | Copy-pasted code from the web |
Step 1 — Read the arrow, not the whole file
Python points at the exact line:
File "main.py", line 4
print("hello")
^
IndentationError: expected an indented block after 'if' statement on line 3
Two facts hide in that message: the broken line (4) and the line that created the expectation (3). The fix is almost always between them.
Step 2 — Fix "expected an indented block"
You wrote a line ending in : and then did not indent the next line.
# Broken
age = 20
if age >= 18:
print("You can vote") # not indented
Every blockA group of lines that run together, marked by indentation after a colon must be indented — four spaces is the Python standard:
# Fixed
age = 20
if age >= 18:
print("You can vote")
Sometimes you genuinely want an empty block — while sketching out a program, for instance. Use pass, a keyword that means "do nothing on purpose":
def send_email():
pass # TODO: write this later
Step 3 — Fix "unexpected indent"
Here a line is indented although nothing above it opened a block:
# Broken
name = "Aditi"
print(name) # why is this pushed in?
Delete the leading spaces:
# Fixed
name = "Aditi"
print(name)
This bites hardest when pasting from a blog or a PDF, which often carry invisible leading spaces along with the code.
Step 4 — Fix "unindent does not match any outer indentation level"
This one confuses people because the line looks fine. Python is saying: "you moved back out to a level that does not exist above you."
# Broken — 4 spaces, then 2 spaces
def greet(name):
if name:
print("Hi", name)
print("Done") # 2 spaces matches nothing
Pick one indent width and use it everywhere:
# Fixed — everything in multiples of 4
def greet(name):
if name:
print("Hi", name)
print("Done")
A quick way to see the damage is to make whitespace visible. In VS Code: View → Render Whitespace → All. Spaces show as dots, tabs as arrows, and the mismatch becomes obvious.
Step 5 — Kill tabs before they kill your afternoon
TabError appears when one line uses a Tab character and another uses spaces. They can look identical on screen and Python still refuses.
Fix it once, permanently, in VS Code:
- Open File → Preferences → Settings
- Search Insert Spaces and turn it on
- Search Tab Size and set it to 4
- Open the broken file, press
Ctrl+Shift+P, run Convert Indentation to Spaces
If you prefer the command line, let a formatter do it for the whole project:
python -m pip install black
python -m black .
BlackA tool that rewrites your code into one consistent style reformats every file to 4-space indentation. Most indentation bugs disappear the first time you run it.
Step 6 — Check the line above when nothing looks wrong
If the reported line seems perfect, look one line up. An unclosed bracket makes Python read two lines as one:
# Broken — missing closing bracket on line 1
numbers = [1, 2, 3
print("done") # error is reported here
Python was still reading the list when it hit print. Close the bracket and both lines are fine again. Unbalanced (, [, { and unterminated quotes all produce this "the error is one line late" effect.
A quick self-check routine
When an IndentationError appears, run through this in order:
- Read which line Python names, and which line it says opened the block
- Is the previous line ending in
:followed by an indented line? - Are all indents a multiple of 4 spaces?
- Are there tabs? Render whitespace and look
- Is there an unclosed bracket or quote just above?
Four out of five times it is number 3 or number 4.
What you learned
- Indentation is syntax in Python, not style
- Each message names a specific, different mistake
- 4 spaces everywhere, never tabs, is the whole prevention strategy
passfills a block you have not written yet- An unclosed bracket reports its error on the next line
FAQ
What causes IndentationError in Python?
Python decides which lines belong together by counting leading spaces. The error appears when a block after a colon is not indented, when a line is indented for no reason, or when indent widths are mixed within one file.
How do I fix "expected an indented block"?
Indent the line after your if, for, while, or def statement by four spaces. If the block is deliberately empty, put pass inside it.
Should I use tabs or spaces in Python?
Use four spaces. The official PEP 8 style guide recommends spaces, and mixing tabs with spaces raises TabError even when the code looks correctly aligned.
Why does my code look correctly indented but still error?
You probably mixed tab characters with spaces, or an unclosed bracket on an earlier line merged two lines together. Turn on whitespace rendering in your editor to see the difference.
How do I fix indentation across a whole project at once?
Install Black with python -m pip install black and run python -m black . in your project folder. It rewrites every file to consistent four-space indentation.