Python errors › IndentationError
IndentationError: unindent does not match any outer indentation level
Updated
The Python error "unindent does not match any outer indentation level" means a line was moved back to a column no block started at. How to find it, and the fix.
What you see
def total(prices):
result = 0
for price in prices:
result += price
return result
print(total([3, 4])) File "/home/me/project/main.py", line 5
return result
^
IndentationError: unindent does not match any outer indentation levelWhat it means
Python remembers the depth at which each open block started: 0, 4 and 8 spaces in the example. When a line moves back to the left, it must land exactly on one of those depths. The return sits at 2 spaces, which matches none of them.
The error points at the line that moved back, and that is the line to fix.
Why it happens
- One or two stray spaces, often after editing by hand.
- Tabs and spaces that look aligned but are not. A tab can display as two, four or eight columns, depending on the editor.
- Code pasted from somewhere with a different indentation width.
How to fix it
Move the line to a depth that exists. Here return belongs to the function, so it goes back to 4 spaces:
def total(prices):
result = 0
for price in prices:
result += price
return result
When the line looks right but still fails, make whitespace visible. Most editors have a "render whitespace" setting. In a terminal, cat -et main.py on macOS or cat -A main.py on Linux shows tabs as ^I.
Python ships a checker for ambiguous indentation:
python3 -m tabnanny main.py
How each Python version words it
| Python | Prints |
|---|---|
| 3.9–3.15 | IndentationError: unindent does not match any outer indentation level |
Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on October 25, 2026.
The wording has been the same for every version here.
Questions
Why does the message not say which level it expected?
It only knows the line matches none of the open levels; any of them could be the one you meant. The open levels are the indentation of the lines above with a colon at the end.
Can a formatter fix it?
Formatters such as Black and Ruff need code that parses, so they stop on this error. Fix the one line first, then let the formatter normalise the rest.
Sources
- Lexical analysis: indentation, Python language reference
- tabnanny: detection of ambiguous indentation, Python documentation
Read on October 25, 2026.