IndentationError

Python SystemParser ErrorCommonLast updated: June 29, 2026Tested on:CPython v3.12PIP v24.0June 2026

This error occurs when you violate Python's strict spacing rules, such as mixing tabs and spaces or using inconsistent indentation depths.

IndentationError Quick Fix⏱️ Est. Fix Time: 2 minutes

Usually happens because:

  • Mixed tab and space characters in the same block
  • Missing indentation below block header statements
  • Inconsistent spacing depth within same nested scope

🔍 Quick Checklist:

What is IndentationError?

Python uses indentation levels instead of curly braces to define code blocks (functions, loops, conditions). An 'IndentationError' is raised during the parsing phase when the interpreter encounters unexpected spaces or tabs, such as a child statement that is not indented further than its parent block, or mixing tabs and spaces on the same line, which is prohibited in Python 3.

Common Causes

  • Mixing tabs and spaces: Using tab characters in some lines and spaces in others within the same code block.
  • Unexpected indentation: Indenting a line of code (such as a variable declaration) that does not start a new nested block.
  • Missing block indentation: Forgetting to indent code lines directly following a header statement like 'def', 'if', or 'for'.
CauseFrequency
Mixing tab characters with space keys in same file⭐⭐⭐⭐⭐
Missing indentation under class/def/if headers⭐⭐⭐⭐
Inconsistent indentation depths (e.g. 3 vs 4 spaces)⭐⭐⭐

Common Mistakes

  • Copying code snippets from web browser tutorials that mix spacebar entries with raw Tab margins.
  • Assuming spaces count doesn't matter as long as lines are indented (blocks must have identical spacing depths; mixing 2-space and 4-space margins triggers errors).

How to Fix

1Normalize to spaces only: Configure your editor to automatically convert tabs to spaces (standard PEP 8 uses 4 spaces).
2Re-align block statement margins: Verify that all code statements within the same block level align to the exact same column.
3Run with tab detection flags: Use python's tabnanny library to detect ambiguous indentation states.

Python Operations & Verification

Align all child statements under a control flow block with consistent space columns.

Standard Block Alignment Example
# Wrong: throws IndentationError
# if True:
#   print("First line")
#     print("Second line - mismatched")

# Correct: PEP 8 compliant 4-space alignment
if True:
    print("First line")
    print("Second line")

Platform Specific Fixes

Display hidden tabs and spaces formatting characters using cat utility flags.

Linux Config
# -T shows tabs as ^I, -E shows line ends as $
cat -A script.py

Best Practices

  • Configure your text editor to display whitespace symbols (spaces as dots, tabs as arrows) to locate formatting discrepancies visually.
  • Run code formatters (like Black or YAPF) to automatically re-align code margins on file save.

Frequently Asked Questions (FAQ)

Q: Why does Python require strict indentation?

Python uses indentation to define block structures instead of braces '{}' or keywords like 'end'. This keeps code clean and readable, but makes margins critical.

Q: How do I fix 'TabError: inconsistent use of tabs and spaces'?

This is a subclass of IndentationError. Set your text editor to 'Insert spaces instead of tabs' or 'Translate tabs' and re-save the file. You can also run 'python -m tabnanny <file>' to find where they are mixed.

Q: How many spaces should I use for indentation?

The official Python style guide (PEP 8) recommends using exactly 4 spaces per indentation level. Never use tabs.

Q: Why did copying code from the internet cause this error?

Boilerplate code copied from websites often mixes tabs and spaces. Select all code in your editor and run the 'Format Document' or 'Convert Indentation to Spaces' utility.

Still having this problem?

Didn't solve your problem?

SuggestRequest Error