← All fixes
IndentationError: unexpected indent / expected an indented block (Python)
#149Error
IndentationError: unexpected indent (or IndentationError: expected an indented block after 'if' statement on line N)
Why it happens, and why this works
Python uses indentation to know where blocks start and end, so a line indented differently from its neighbours, or a colon followed by no indented line, can't be parsed. Mixed tabs and spaces look aligned in an editor but count differently to Python.
Fix
Make each block's indentation consistent and use spaces only. 1. Go to the line in the error (and the one above it): an 'unexpected indent' line is indented more than the code around it; 'expected an indented block' means the line after a colon (if/for/def/class/with/try) isn't indented, or the block is empty. 2. Empty block on purpose: put pass in it. 3. Convert tabs to 4 spaces in the whole file (editor: Convert Indentation to Spaces; or python -m tabnanny file.py to find mixed lines). 4. Code pasted into a REPL or from a chat often carries extra leading spaces; dedent it first (textwrap.dedent for strings).
Did it work?
No reports yet0 worked0 didn't
Tried it yourself?
Related fixes
IndentationError: expected an indented block after function definition
#99IndentationError: expected an indented block after function definition on line 1
pythonVerifiedNo reports yet
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes
#50json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
pythonVerifiedNo reports yet
TabError: inconsistent use of tabs and spaces in indentation
#68TabError: inconsistent use of tabs and spaces in indentation
pythonVerifiedNo reports yet