IndentationError: unexpected indent

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

SituationWhy it errorsFix
indent with no : abovenothing opened a blockremove the leading whitespace
over-indented body linedoesn't match siblingsalign to the block level
tab + space mixinconsistent indentationspaces only; convert tabs
pasted codeauto-indent / injected spacesre-indent the pasted block
deleted a header lineorphaned indented bodydedent the former body

Debugging checklist

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.

All Error References Python: NoneType has no attribute
About the author

Pasindu Ishan is a software developer based in Sri Lanka. He builds privacy-first developer tools at JSON Dev Tools.