Fix "IndentationError: unexpected indent" in Python
IndentationError: unexpected indent
- IndentationError: expected an indented block after 'if' statement on line 3
- IndentationError: expected an indented block
- IndentationError: unindent does not match any outer indentation level
- TabError: inconsistent use of tabs and spaces in indentation
Python found a line indented more than the code structure allows. Align it with the block it belongs to, use spaces only (4 per level) and add pass to empty blocks.
What this error means
In Python, indentation is part of the syntax: it delimits blocks instead of braces. The tokenizer compares each line’s indentation to a stack of open levels. A line indented further without a : right before it gives “unexpected indent”. An if, def, for or class with no indented body gives “expected an indented block”. A dedent that lands on no existing level gives “unindent does not match any outer indentation level”. Finally, Python 3 refuses to guess when tabs and spaces are mixed ambiguously and raises TabError. The error is raised before anything runs, when the file is compiled.
Common causes
- Extra space or tab at the start of a line, often after copy-pasting from a web page, a PDF or a chat.
- Mixed tabs and spaces: the line looks aligned in the editor, but a tab is not equal to 4 spaces for Python.
- Empty block: an
if,def,tryorclasswhose body was deleted or commented out, causing “expected an indented block”. - Inconsistent dedent: a line returns to 2 spaces while the open blocks are at 0 and 4.
- Pasting into the classic REPL: a blank line inside a function ends the block, so the next line becomes “unexpected indent”. The new Python 3.13 REPL handles pasting better.
Step-by-step fix
-
Read the line number and caret
The message gives the file, the line and a
^under the faulty spot. For “expected an indented block”, the problem is the line after the statement ending with:. -
Show spaces and tabs
Make invisible characters visible. In VS Code, add this setting to
settings.jsonor use View, then Render Whitespace.{ "editor.renderWhitespace": "all", "editor.insertSpaces": true, "editor.tabSize": 4 } -
Find ambiguous indentation
The standard
tabnannymodule reports lines where tabs and spaces are mixed. On macOS and Linux,grepcan also list lines containing a tab.python -m tabnanny -v script.py grep -n "$(printf '\t')" script.py -
Convert tabs to spaces
In VS Code: command palette, then “Convert Indentation to Spaces”. On the command line on macOS or Linux,
expandreplaces each tab with spaces.expand -t 4 script.py > script_fixed.py && mv script_fixed.py script.py -
Give empty blocks a body
A block cannot be empty in Python. Use
pass(or...) as a placeholder body.def todo(): pass if user is None: ... else: print(user.name) -
Realign, then format
Once the file is valid, format it with
ruff formatorblack: both enforce 4 spaces and keep the problem from coming back. They cannot format a file that still has a syntax error.python -m pip install ruff ruff format script.py
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Add an
.editorconfigwithindent_style = spaceandindent_size = 4for*.pyfiles. - Enable format on save (Ruff or Black) and a pre-commit hook.
- After a copy-paste, reindent the whole pasted block at once rather than line by line.
Frequently asked questions
How many spaces for indentation in Python?
PEP 8 recommends 4 spaces per level and no tabs. Python accepts other widths as long as you are consistent within a block.
Why does the error appear when the line looks aligned?
It probably contains a tab where the other lines use spaces. Show whitespace in the editor or run python -m tabnanny to find it.
How do I write an empty if block in Python?
Put pass as the only statement in the block. A comment alone is not enough, because it is not a statement.
Useful tools for this error
More Python errors
- Fix "ModuleNotFoundError: No module named" in Python
- Fix "'NoneType' object is not subscriptable" in Python
- Fix "error: externally-managed-environment" with pip
- Fix "UnicodeDecodeError: 'utf-8' codec can't decode byte" in Python
- Fix "KeyError" in Python (dict, pandas, os.environ)
- Fix "RecursionError: maximum recursion depth exceeded" in Python
- Fix "attempted relative import with no known parent package" in Python