ErrorPython

IndentationError: unexpected indent

Why Python raises IndentationError: unexpected indent, how mixed tabs and spaces cause it invisibly, and how to fix the offending line for good.

Last updated

ErrorPython
  File "script.py", line 4
    print("hello")
IndentationError: unexpected indent

What it means: Python found a line indented further than the surrounding code expects, with nothing before it — such as a `:` opening a new block — to justify the deeper indentation.

Likely causes

Most probable first — with how to confirm.

  • 1.Mixed tabs and spaces within the same file — they can look identical in some editors, but Python 3 treats inconsistent tab/space mixing within a block as an error; check your editor's 'show whitespace' setting.
  • 2.A stray extra space or two at the start of a line, often from pasting code copied out of a webpage, PDF, or a source with a different indent width — check the exact column of the flagged line against the lines around it.
  • 3.The line is indented under a statement that doesn't actually open a block — a missing `:` on the preceding `if`, `for`, or `def` line — or the line simply doesn't belong indented at all.

Fixes

Safest first; destructive ones are called out.

  • →Turn on 'render whitespace' in your editor and fix the offending line to match the indentation of the block it belongs to — the fastest fix once tabs and spaces are visible.
  • →Standardize on spaces (PEP 8 recommends 4, no tabs) and set your editor to insert spaces on Tab; most editors can also auto-convert an existing file's tabs to spaces.
  • →Run `python -m tabnanny script.py` to locate ambiguous indentation across a whole file. (The old `-tt` flag is a Python 2 relic — Python 3 accepts it as a silent no-op and raises TabError on its own regardless.)
  • →If the code was pasted from an external source, re-indent that block by hand rather than trusting whatever whitespace came along with the paste.

Part by part

File "script.py", line 4
Where Python stopped. The real problem is frequently the line above, since indentation is only wrong relative to what came before it.

Common mistakes

  • Pressing tab until it lines up

    Visually aligned is not the same as consistently indented. A file mixing tabs and spaces can look perfect and still fail, because Python compares the characters rather than the rendered width.

  • Setting the editor to display tabs as four spaces

    Changes the appearance and not the bytes, so the file still contains tabs. Configure the editor to insert spaces instead — for Python, four of them, per PEP 8.

  • Reindenting the whole file to make the error go away

    Often converts a small local mistake into a large diff that hides it. Fix the block the traceback points at, then run a formatter deliberately as a separate change.

Questions

How do I find mixed tabs and spaces?

python -m tabnanny on the file reports ambiguous indentation with line numbers. Most editors can also render whitespace, which makes the offending line visible immediately.

Why does the error point at a line that looks fine?

Because Python reports where the indentation became inconsistent, not where the mistake was made. An unclosed bracket on an earlier line produces the same effect, since the parser is still inside that expression.

Tabs or spaces?

Spaces, four of them, is the PEP 8 recommendation and what virtually every Python project uses. The real rule is consistency within a file — Python 3 refuses to mix them at all, which removes the argument.

How do I stop this happening?

Let a formatter own it. Running black or ruff format on save makes indentation a non-decision, and an .editorconfig ensures everyone's editor inserts the same characters in the first place.

Related in Errors