Fix IndentationError in Python: 6 Causes and Exact Fixes (2026)
Python IndentationError means your whitespace is inconsistent or missing. Here are the 6 most common causes with exact code fixes including mixed tabs/spaces, empty blocks, and VS Code settings that prevent it permanently.
The Error
or
or
Python uses indentation as syntax. Unlike other languages where indentation is style, Python requires it to be correct and consistent. Every IndentationError means the interpreter found whitespace it could not parse.
The 6 Most Common Causes
1. Empty Function or Class Body
Python expects at least one statement inside every def, class, if, for, while, or with block. An empty block is a syntax error.
Add pass as a placeholder:
2. Mixed Tabs and Spaces
Your editor shows this looking fine. Python 3 does not allow mixing tabs and spaces in the same file. It throws TabError.
Convert all indentation to spaces. In VS Code, open Command Palette and run Convert Indentation to Spaces. Set tab size to 4.
Add this to your project's .editorconfig to enforce it for everyone:
3. Wrong Level of Dedent
The return is indented at a level that does not match any containing block. Python cannot figure out where it belongs.
Align the dedent to a real block level:
4. Copy-Pasted Code With Wrong Indentation
Pasting from documentation, Stack Overflow, or a chat tool often introduces wrong indentation or invisible characters. The code looks correct visually but Python disagrees.
Select all pasted code, press Tab, then Shift+Tab to normalize indentation. Or run Reindent Lines from the VS Code Command Palette.
5. Continuation Line Not Indented
Inside parentheses, brackets, or braces, Python allows implicit line continuation, but continuation lines must still be indented relative to the opening.
6. Comment After Colon With No Real Body
A comment does not count as the indented block. Python still expects a real statement.
How to Find It Fast
Python's IndentationError includes the file and line number. Go directly there.
Enable editor.renderWhitespace in VS Code settings to make spaces and tabs visible. Tabs show as arrows, spaces show as dots. This instantly reveals mixed indentation:
Run ruff check . to catch IndentationError and whitespace issues before you run your code.
Prevent It Permanently
Add this to your project root as .editorconfig:
Add this to VS Code settings.json:
detectIndentation: false stops VS Code from guessing indent style from file content, which is what causes the mixed-indent problem when pasting code.
FAQ
Q: Why does my code look correctly indented but Python still throws IndentationError?
A: Invisible characters are almost always the cause. Paste the file into a hex editor or run cat -A file.py in the terminal to see tabs as ^I and spaces as regular characters. VS Code's renderWhitespace: all setting makes this visible without leaving the editor.
Q: Does Black or Ruff fix IndentationError automatically?
A: Black reformats indentation as part of its standard run, so yes for most cases. Ruff with the --fix flag handles many whitespace issues too. Run black . on your project and most IndentationError caused by inconsistent spacing will be resolved.
For IndentationError that is harder to trace across files, paste the error into DebugAI and it will read your terminal output, query the relevant files, and find the exact line and root cause.
Debug faster starting today.
Free VS Code extension. 10 sessions/day. No credit card.