On this page

Tutorial5 min read

Fix IndentationError in Python: 6 Causes and Exact Fixes (2026)

Python IndentationError means your whitespace is inconsistent or missing. Here are the 6 most common causes with exact code fixes including mixed tabs/spaces, empty blocks, and VS Code settings that prevent it permanently.

PythonIndentationErrordebuggingVS Code

The Error

IndentationError: expected an indented block

or

IndentationError: unindent does not match any outer indentation level

or

TabError: inconsistent use of tabs and spaces in indentation

Python uses indentation as syntax. Unlike other languages where indentation is style, Python requires it to be correct and consistent. Every IndentationError means the interpreter found whitespace it could not parse.

The 6 Most Common Causes

1. Empty Function or Class Body

python
# Empty body is illegal
def my_function():

def next_function():
    pass

Python expects at least one statement inside every def, class, if, for, while, or with block. An empty block is a syntax error.

Add pass as a placeholder:

python
def my_function():
    pass

2. Mixed Tabs and Spaces

python
def calculate_total(items):
    total = 0
      for item in items:    # this line uses a tab, rest use spaces
        total += item.price
    return total

Your editor shows this looking fine. Python 3 does not allow mixing tabs and spaces in the same file. It throws TabError.

Convert all indentation to spaces. In VS Code, open Command Palette and run Convert Indentation to Spaces. Set tab size to 4.

Add this to your project's .editorconfig to enforce it for everyone:

[*.py]
indent_style = space
indent_size = 4

3. Wrong Level of Dedent

python
def process_data(data):
    if data:
        result = transform(data)
     return result     # IndentationError: unindent does not match any outer indentation level

The return is indented at a level that does not match any containing block. Python cannot figure out where it belongs.

Align the dedent to a real block level:

python
def process_data(data):
    if data:
        result = transform(data)
    return result     # 4 spaces, aligns with the if

4. Copy-Pasted Code With Wrong Indentation

Pasting from documentation, Stack Overflow, or a chat tool often introduces wrong indentation or invisible characters. The code looks correct visually but Python disagrees.

Select all pasted code, press Tab, then Shift+Tab to normalize indentation. Or run Reindent Lines from the VS Code Command Palette.

5. Continuation Line Not Indented

python
result = (
value_1 +    # IndentationError — continuation line must be indented
value_2
)

Inside parentheses, brackets, or braces, Python allows implicit line continuation, but continuation lines must still be indented relative to the opening.

python
result = (
    value_1 +
    value_2
)

6. Comment After Colon With No Real Body

python
for item in items:
# comment here, then nothing
result = process(item)    # IndentationError: expected an indented block

A comment does not count as the indented block. Python still expects a real statement.

python
for item in items:
    result = process(item)

How to Find It Fast

Python's IndentationError includes the file and line number. Go directly there.

Enable editor.renderWhitespace in VS Code settings to make spaces and tabs visible. Tabs show as arrows, spaces show as dots. This instantly reveals mixed indentation:

json
{
  "editor.renderWhitespace": "all"
}

Run ruff check . to catch IndentationError and whitespace issues before you run your code.

Prevent It Permanently

Add this to your project root as .editorconfig:

root = true

[*.py]
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

Add this to VS Code settings.json:

json
{
  "editor.tabSize": 4,
  "editor.insertSpaces": true,
  "editor.detectIndentation": false
}

detectIndentation: false stops VS Code from guessing indent style from file content, which is what causes the mixed-indent problem when pasting code.

FAQ

Q: Why does my code look correctly indented but Python still throws IndentationError?

A: Invisible characters are almost always the cause. Paste the file into a hex editor or run cat -A file.py in the terminal to see tabs as ^I and spaces as regular characters. VS Code's renderWhitespace: all setting makes this visible without leaving the editor.

Q: Does Black or Ruff fix IndentationError automatically?

A: Black reformats indentation as part of its standard run, so yes for most cases. Ruff with the --fix flag handles many whitespace issues too. Run black . on your project and most IndentationError caused by inconsistent spacing will be resolved.


For IndentationError that is harder to trace across files, paste the error into DebugAI and it will read your terminal output, query the relevant files, and find the exact line and root cause.

Debug faster starting today.

Free VS Code extension. 10 sessions/day. No credit card.

Install Free →

Related Posts

Tutorial

How to Debug a Next.js Application in VS Code (Complete Guide)

8 min read

Tutorial

How to Debug a FastAPI Application (Complete VS Code Guide)

9 min read

← All posts