IndentationError 🐍 Python

IndentationError: unexpected indent / expected an indented block

Python’s block structure is broken — inconsistent indentation or mixed tabs and spaces.

Seen on: Python

Meaning

Indentation is syntax in Python. Every block after : must be indented consistently, and mixing tabs and spaces (TabError) is not allowed.

Common causes

  • Mixed tabs and spaces
  • Empty block after if/def/for (use pass)
  • Copy-pasted code with different indentation
  • Stray indentation on a top-level line

⚡ Quick fix

  1. Configure the editor to insert 4 spaces
  2. Convert tabs to spaces for the file
  3. Add pass to empty blocks

Detailed fix by platform

Python

  1. python -m tabnanny file.py reports ambiguous indentation.

Code examples

Empty block needs pass

python
def todo():
    # ❌ IndentationError: expected an indented block
def todo():
    pass        # ✅

Convert tabs to spaces

bash
python -m tabnanny app.py           # reports ambiguous lines
expand -t 4 app.py > app_fixed.py   # tabs → 4 spaces

How to diagnose

  1. Line — Reported line and the block header above it
  2. Whitespace — Show invisible characters in your editor — tabs vs spaces?
  3. Empty blocks — Any if/def/for with no body?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.