Skip to main content

Python · 5 common causes

IndentationError: expected an indented block

Updated

A statement that opens a block — if, for, while, def, class, try — has nothing indented underneath it.

The exact message

  File "/app/main.py", line 4
    print("done")
    ^
IndentationError: expected an indented block after 'if' statement on line 3

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: expected an indented block.

How to fix it

Cause 1 of 5

The block body is missing entirely.

Indent at least one statement under the header line. If the body is genuinely meant to be empty for now, write pass — it is a real statement that does nothing and exists precisely for this.

Cause 2 of 5

The body is written at the same indentation as the header.

Everything belonging to the block must be indented further than the line ending in a colon. Add one level (four spaces) to every line of the body, not just the first.

Cause 3 of 5

Only a comment sits under the header.

Comments are not statements, so a block containing nothing but # TODO is still empty. Put pass under the comment.

Cause 4 of 5

Tabs and spaces disagree, so the body looks unindented to the parser.

Convert the file to spaces and reindent. A body indented with a tab, under a header indented with spaces, can render aligned and still fail to parse.

Cause 5 of 5

The colon is on the wrong line after an edit.

Check the header itself: deleting a line can leave if condition: immediately followed by a dedented else: or the end of the file. Python 3.10 and newer name the offending statement and its line number in the message, which points straight at it.

Stopping it coming back

Write pass the moment you type a header you are not ready to fill in, and let a formatter and linter run on save so an empty block is caught as you write it.

IndentationError: expected an indented block — common questions

Can I put the body on the same line as the if?
For a single simple statement, yes: if x: return 1 is valid. It is discouraged by PEP 8 and cannot hold multiple statements or nested blocks, so use it sparingly if at all.
Why does the message name a line before the error?
Because the header is what created the expectation. Python 3.10 improved these messages to say which statement was left without a body, so the number in the message is the block opener and the caret is where it noticed.
Is pass the same as ...?
Both are valid empty bodies. pass is the conventional placeholder in ordinary code; the Ellipsis literal ... is idiomatic in type stubs and protocol definitions. Neither does anything at runtime.

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