Skip to main content

Python · 4 common causes

IndentationError: unexpected indent

Updated

A line is indented further than the line before it without anything having opened a new block.

The exact message

  File "/app/main.py", line 4
    print(total)
IndentationError: unexpected indent

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is IndentationError: unexpected indent.

How to fix it

Cause 1 of 4

The line has stray leading whitespace.

Line it up with the statement above it. The traceback prints the file and the line number, and the caret marks the column — the fix is almost always on that exact line, not before it.

Cause 2 of 4

Tabs and spaces are mixed in the same block.

Python 3 rejects inconsistent indentation even when it looks aligned on screen. Set the editor to insert 4 spaces per indent and convert the file (in VS Code: Convert Indentation to Spaces); PEP 8 is 4 spaces and no tabs. Python 3 raises TabError for an ambiguous tab/space mix on its own, so no extra flag is needed; python -t was a Python 2 option and is a silent no-op here. To see which character is which, switch on the editor's render-whitespace setting, or run cat -A script.py where a tab prints as ^I.

Cause 3 of 4

Code was pasted from a document, terminal or notebook with its own indentation.

Select the pasted block and reindent it as a unit — most editors do this with Shift+Tab or a Reindent Lines command. Pasting into a REPL adds its own layer of trouble; use %paste in IPython or paste into a file instead.

Cause 4 of 4

You indented a line hoping to continue the previous statement.

Python does not join lines by indentation. Wrap the whole expression in parentheses — the contents can then span as many lines as you like — or, less readably, end the previous line with a backslash and no trailing space.

Stopping it coming back

Let a formatter own indentation entirely: run Black or Ruff on save, and enable the editor setting that renders whitespace so a tab is visible before you commit it.

IndentationError: unexpected indent — common questions

Why does the line look correctly aligned in my editor?
Because a tab renders as whatever width your editor is set to, so a tab-indented line and a space-indented line can appear identical while being different to the parser. Turn on whitespace rendering and the difference becomes obvious immediately.
What is TabError and how does it differ?
TabError: inconsistent use of tabs and spaces in indentation is the specific message Python gives when the mix is unambiguous enough to name. It has the same cure — pick spaces and convert the file.
Does the amount of indentation matter?
Only that it is consistent within a block. Four spaces is the convention and every Python codebase you will read uses it, but two or eight would parse. Mixing widths inside one block will not.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor