Quick answer
A line has more leading whitespace than expected, but nothing opened a new block. Python only allows indentation to increase right after a line ending in a colon (def, if, for, while, class, try, with). Delete the stray indent so the line aligns with its siblings — and use spaces only, never mixed with tabs.
The exact error string
File "app.py", line 3
print("done")
^
IndentationError: unexpected indent
How to diagnose it in 30 seconds
The traceback gives you the exact file and line, and the ^ caret sits under the first offending column. Ask one question: did the line above end in a colon? If no, this line should not be more indented — remove the extra whitespace. If yes, the indent is fine and your real problem is elsewhere (often mixed tabs/spaces, see below).
Cause 1: stray indentation with no block
x = 1
print(x) # ❌ indented, but nothing opened a block
# ✅ align it with the previous statement
x = 1
print(x)
Cause 2: over-indented line inside a block
def greet(name):
print("hi")
print(name) # ❌ extra indent — same level as line above expected
# ✅ match the block's indentation
def greet(name):
print("hi")
print(name)
Cause 3: mixed tabs and spaces
def total(items):
→ return sum(items) # this line uses a TAB
print("done") # this line uses SPACES -> inconsistent
# ✅ pick spaces everywhere (PEP 8: 4 spaces) and convert tabs
# Editors: "Convert Indentation to Spaces", show whitespace
def total(items):
return sum(items)
If tabs and spaces collide you may instead see TabError: inconsistent use of tabs and spaces in indentation — same root cause, same fix. Run python -tt app.py to make Python treat inconsistent tabs/spaces as errors everywhere.
Cause 4: leftover indent after editing
if debug:
log("on")
cleanup() # removed the `if`, but left the body indented
# ✅ after deleting a header, dedent its former body
log("on")
cleanup()
Common causes at a glance
| Situation | Why it errors | Fix |
|---|---|---|
indent with no : above | nothing opened a block | remove the leading whitespace |
| over-indented body line | doesn't match siblings | align to the block level |
| tab + space mix | inconsistent indentation | spaces only; convert tabs |
| pasted code | auto-indent / injected spaces | re-indent the pasted block |
| deleted a header line | orphaned indented body | dedent the former body |
Debugging checklist
- ✓ Go to the reported line; look at the
^column - ✓ Did the line above end in
:? If not, remove the indent - ✓ Turn on "show whitespace" — look for stray tabs
- ✓ Convert the file to spaces only (4-space indent)
- ✓ Run
python -tt app.pyto flag any tab/space mixing - ✓ Just edited? Check for an orphaned body after a deleted header
Frequently Asked Questions
What does 'IndentationError: unexpected indent' mean?
A line has more leading whitespace than the line before it, but nothing opened a new block. In Python, indentation only increases right after a statement ending in a colon (def, if, for, while, class, try, with). An indent anywhere else is unexpected.
What's the difference between 'unexpected indent' and 'expected an indented block'?
"unexpected indent" is too much whitespace where none was allowed. "expected an indented block" is the opposite — a colon line (def/if/for) wasn't followed by an indented body. One is extra indentation, the other is missing indentation.
Why do mixed tabs and spaces cause this?
Python compares indentation by characters, and a tab is not the same as N spaces. Lines that look aligned in your editor can differ underneath, producing "unexpected indent" or the related TabError: inconsistent use of tabs and spaces. Use spaces only (PEP 8: 4 spaces) and never mix the two.
How do I fix unexpected indent fast?
Go to the line and column the traceback points at, delete the stray leading whitespace so the line aligns with its siblings, and make sure any real block is opened by a colon line above it. Turn on "show whitespace" and convert tabs to spaces in your editor.
Why does it appear after pasting code?
Pasting into a REPL or editor often injects leading spaces, auto-indents continuation lines, or mixes the source's tabs with your editor's spaces. Re-indent the pasted block consistently, or paste with bracketed-paste / a paste-friendly mode in your terminal.
Does the indent amount have to be exactly 4 spaces?
Python only requires that indentation be consistent within a block — every statement at the same level must use identical leading whitespace. 4 spaces is the PEP 8 convention and what most tools assume, so it's the safe default, but the language enforces consistency, not a specific count.
More Python errors
Browse the full reference for Python, JavaScript, Go, and database errors — exact message, cause, and fix.