Home › Python

IndentationError: unindent does not match any outer indentation level (Python) — how to fix

File "app.py", line 6
  return total
              ^
IndentationError: unindent does not match any outer indentation level

Quick fix: Go to the reported line and make its indentation exactly match the block it belongs to. Turn on 'show whitespace' in your editor, convert tabs to spaces (VS Code: 'Convert Indentation to Spaces'), or run python -m tabnanny app.py to find the bad lines.

What it means

Python tracks a stack of indentation levels. When a line has less indentation than the previous one, it must return to exactly one of the levels already on that stack. Here it landed in between:

def total(items):
    total = 0              # 4 spaces
    for i in items:
        total += i         # 8 spaces
   return total            # 3 spaces: matches neither 0 nor 4

The line in the traceback is the one that dedents wrongly. Sometimes the actual mistake is on a line above it, which set up a level you didn't intend.

Cause 1: inconsistent number of spaces

Code pasted from a website, an email or a chat often arrives with 2- or 3-space indentation mixed into a 4-space file.

Fix 1

Re-indent the block so every line at the same level uses the same number of spaces. The simplest way is to select the whole function in your editor, dedent everything to column 0, and indent it again. PEP 8 recommends 4 spaces per level.

An auto-formatter fixes this in one go once the file parses:

pip install black
black app.py

(black can't format a file that has a syntax error, so fix the reported line first if it complains.)

Cause 2: tabs mixed with spaces

A tab and four spaces look identical in most editors but are different characters. Python 3 compares them strictly, so a tab-indented line under a space-indented one may not match any level. Depending on the exact mix you get this error or TabError: inconsistent use of tabs and spaces in indentation.

Fix 2

Make the invisible visible and convert:

From the command line, tabnanny reports ambiguous lines and expand replaces tabs:

python -m tabnanny -v app.py
expand -t 4 app.py > app_fixed.py && mv app_fixed.py app.py

You can also check for tab characters with grep -nP '\t' app.py, or paste the code into the whitespace inspector to see tabs and spaces marked.

Cause 3: an else, except or elif at the wrong level

The clause must line up exactly with its if / try:

try:
    run()
  except ValueError:     # 2 spaces: wrong
    pass

Fix 3

try:
    run()
except ValueError:
    pass

Cause 4: editing in the REPL or a notebook

In the interactive interpreter, pasted code with blank lines inside a block or leading spaces can trigger this. In Jupyter, a cell copied from elsewhere may contain tabs.

Fix 4

Paste the code into a .py file and run it, or retype the indentation in the cell.

Prevent it

Set your editor to insert spaces for Tab and to show indentation guides. An .editorconfig in the project keeps everyone consistent:

[*.py]
indent_style = space
indent_size = 4

Last updated 2026-11-30.