Skip to content
FR
  1. LuzineDebug
  2. Python
  3. IndentationError: unexpected indent
Python Beginner ≈ 3 min to fix

Fix "IndentationError: unexpected indent" in Python

Error message
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
In short

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.

Be the first to confirm this fix

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

  1. Extra space or tab at the start of a line, often after copy-pasting from a web page, a PDF or a chat.
  2. Mixed tabs and spaces: the line looks aligned in the editor, but a tab is not equal to 4 spaces for Python.
  3. Empty block: an if, def, try or class whose body was deleted or commented out, causing “expected an indented block”.
  4. Inconsistent dedent: a line returns to 2 spaces while the open blocks are at 0 and 4.
  5. 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

  1. 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 :.

  2. Show spaces and tabs

    Make invisible characters visible. In VS Code, add this setting to settings.json or use View, then Render Whitespace.

    json
    {
      "editor.renderWhitespace": "all",
      "editor.insertSpaces": true,
      "editor.tabSize": 4
    }
  3. Find ambiguous indentation

    The standard tabnanny module reports lines where tabs and spaces are mixed. On macOS and Linux, grep can also list lines containing a tab.

    bash
    python -m tabnanny -v script.py
    grep -n "$(printf '\t')" script.py
  4. Convert tabs to spaces

    In VS Code: command palette, then “Convert Indentation to Spaces”. On the command line on macOS or Linux, expand replaces each tab with spaces.

    bash
    expand -t 4 script.py > script_fixed.py && mv script_fixed.py script.py
  5. Give empty blocks a body

    A block cannot be empty in Python. Use pass (or ...) as a placeholder body.

    python
    def todo():
        pass
    
    if user is None:
        ...
    else:
        print(user.name)
  6. Realign, then format

    Once the file is valid, format it with ruff format or black: both enforce 4 spaces and keep the problem from coming back. They cannot format a file that still has a syntax error.

    bash
    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 .editorconfig with indent_style = space and indent_size = 4 for *.py files.
  • 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

All Python errors →