Python IndentationError: Why It Happens and How to Fix It
IndentationError means Python found indentation that does not match what it expects. Unlike most languages, Python uses indentation instead of braces to define code blocks. The most common cause is mixing tabs and spaces in the same file. The fix is to pick one (spaces are the standard) and convert the entire file.
Why Python cares about indentation
In JavaScript or Java, you use curly braces to define blocks of code. In Python, indentation replaces braces. When you write an if statement, Python decides which lines belong to that block by their indentation level:
if score >= 50:
print("Pass") # indented: inside the if block
send_certificate() # indented: also inside
print("Done") # not indented: outside the blockIf a line is indented by a different amount than Python expects, or uses a different character (tab vs space), Python throws an IndentationError and stops.
Common causes
1. Mixing tabs and spaces. This is the most frequent cause. Your editor might insert a tab character on one line and four spaces on the next. They look identical on screen but Python sees them as different.
2. Inconsistent indentation levels. One line uses two spaces, the next uses four. Python expects every line in the same block to match exactly.
3. Copy-pasting code. Code copied from a webpage, PDF, or Stack Overflow answer often carries invisible characters or mixed whitespace.
4. Empty block after a colon. If you write if True: and the next line is not indented, Python complains. Use pass as a placeholder if the block is intentionally empty.
if debug_mode:
pass # placeholder, will add logging laterHow to fix it
Step 1: Make invisible characters visible. In VS Code, open the command palette (Ctrl+Shift+P) and run "View: Toggle Render Whitespace" to see dots for spaces and arrows for tabs. This reveals exactly where the inconsistency is.
Step 2: Convert the entire file to spaces. In VS Code, click the "Spaces: 4" or "Tab Size" indicator in the bottom status bar. Select "Convert Indentation to Spaces". Use 4 spaces per level, which is the Python standard (PEP 8).
Step 3: Re-indent the offending block. If a single block is misaligned, select the lines and use Tab to indent or Shift+Tab to unindent until they match the surrounding code.
Configuring your editor to prevent this
Set your editor to insert spaces when you press the Tab key and to use 4 spaces per indent level for Python files.
VS Code: Add to your settings.json:
{
"[python]": {
"editor.tabSize": 4,
"editor.insertSpaces": true,
"editor.detectIndentation": false
}
}PyCharm: Go to Settings > Editor > Code Style > Python. Set "Use tab character" to unchecked and "Tab size" to 4.
Sublime Text: Go to Preferences > Settings and add:
"translate_tabs_to_spaces": true,
"tab_size": 4With these settings, every new file you create and every Tab key press will produce spaces consistently.
Frequently Asked Questions
- Should I use tabs or spaces in Python?
- Spaces. PEP 8, the official Python style guide, recommends 4 spaces per indentation level. The Python community follows this almost universally. Tabs work too, but you must never mix them. If you pick spaces and configure your editor, you will never see this error again.
- What is the difference between IndentationError and TabError?
- TabError is a specific subtype of IndentationError that means Python detected tabs and spaces mixed in the same file. The fix is the same: convert the whole file to spaces.
- I pasted code from a tutorial and got an IndentationError. Why?
- Webpages and PDFs often use non-standard whitespace characters that look like spaces but are not. After pasting, select the pasted lines and re-indent them manually. Or use your editor's "Convert Indentation to Spaces" command on the selection.
Ready to build real-world apps?
Join the McTaba Labs full-stack marathon. Ship 8 production apps with M-Pesa, USSD, and WhatsApp integrations, and get career support until placement.
See Programs