# How to Fix Python IndentationError

Python uses indentation to define code blocks, unlike languages that use braces. The IndentationError occurs when your code's indentation doesn't follow Python's strict rules. This is a parse-time error that prevents your script from running.

Error Patterns

Unexpected Indent

text
File "app.py", line 5
    print("Hello")
    ^
IndentationError: unexpected indent

Unindent Does Not Match

text
File "app.py", line 10
        return result
    ^
IndentationError: unindent does not match any outer indentation level

Expected Indented Block

text
File "app.py", line 15
    if x > 10:
                 ^
IndentationError: expected an indented block after 'if' statement on line 15

Mixed Tabs and Spaces

text
File "app.py", line 20
    def func():
            ^
TabError: inconsistent use of tabs and spaces in indentation

Common Causes

  1. 1.Mixed tabs and spaces - Using both in same file
  2. 2.Inconsistent indentation levels - Not matching outer blocks
  3. 3.Missing indentation - After if, def, class, for, while
  4. 4.Extra indentation - Indenting code that shouldn't be indented
  5. 5.Copy-paste issues - Code copied with different indentation
  6. 6.Editor settings - Auto-indent with wrong settings
  7. 7.Indentation depth changes - Accidentally changing indent depth

Diagnosis Steps

Step 1: Check Error Line and Context

python
# Python shows the exact line with issue
# Example:
#   File "app.py", line 10
#         return result
#     ^
# Check lines above and below for indentation patterns

Step 2: Show Whitespace Characters

```bash # In most editors, enable "show whitespace" # VS Code: View -> Render Whitespace # Notepad++: View -> Show Symbol -> Show White Space and TAB

# Or use cat with visible tabs cat -A app.py # Shows tabs as ^I, line ends as $ ```

python
# Python script to show indentation
with open("app.py") as f:
    for i, line in enumerate(f, 1):
        if line.strip():  # Skip empty lines
            # Count leading spaces/tabs
            indent = len(line) - len(line.lstrip())
            tabs = len(line) - len(line.replace('\t', '', 1))
            print(f"Line {i}: indent={indent}, tabs={tabs}, content={repr(line[:30])}")

Step 3: Check Editor Settings

```bash # VS Code settings.json { "editor.tabSize": 4, "editor.insertSpaces": true, "editor.detectIndentation": false }

# PyCharm: Settings -> Editor -> General -> Smart Tabs # Set to use spaces, not tabs ```

Step 4: Use Python to Detect Issues

```python import tokenize import io

def check_indentation(filename): """Check file for indentation issues.""" with open(filename) as f: content = f.read()

# Try to tokenize (will fail on indentation errors) try: tokens = list(tokenize.generate_tokens(io.StringIO(content).readline)) print("No indentation errors detected") except IndentationError as e: print(f"Indentation error at line {e.lineno}:") print(f" {e.msg}")

# Show the problematic area lines = content.split('\n') for i in range(max(0, e.lineno - 3), min(len(lines), e.lineno + 3)): marker = ">>> " if i == e.lineno - 1 else " " print(f"{marker}{i + 1}: {repr(lines[i])}")

check_indentation("app.py") ```

Solutions

Solution 1: Convert Tabs to Spaces

```python # Fix mixed tabs/spaces by converting all tabs to spaces

def convert_tabs_to_spaces(filename, tab_size=4): """Convert all tabs to spaces in a file.""" with open(filename) as f: content = f.read()

# Replace tabs with spaces fixed = content.replace('\t', ' ' * tab_size)

with open(filename, 'w') as f: f.write(fixed)

print(f"Converted tabs to {tab_size} spaces in {filename}")

convert_tabs_to_spaces("app.py") ```

```bash # Or use expand command (Linux/Mac) expand -t 4 app.py > app_fixed.py mv app_fixed.py app.py

# Or use sed sed -i 's/\t/ /g' app.py # Replace tabs with 4 spaces ```

Solution 2: Fix Unexpected Indent

```python # Problem: Indenting code that shouldn't be indented

# Wrong x = 10 print(x) # Unexpected indent - not inside any block

# Fix: Remove extra indentation x = 10 print(x)

# Another common mistake - indenting after pass/break/continue if x > 5: pass print("This is wrongly indented") # Not part of the if block

# Fix: Either remove or make it part of the block if x > 5: pass print("This is outside the if block")

# Or if x > 5: pass # print("Commented out or removed") ```

Solution 3: Fix Unindent Mismatch

```python # Problem: Indentation doesn't match any outer level

def calculate(x): result = x * 2 return result # Wrong indent level (2 spaces instead of 4)

# Fix: Match the function's indentation level def calculate(x): result = x * 2 return result # Same level as other statements in function

# Visual alignment def outer(): if condition: do_something() do_other() # Wrong - doesn't match if or def level do_other() # Correct - matches def level if condition: do_something() do_more() # Correct - matches if level ```

Solution 4: Add Missing Indented Block

```python # Problem: Empty block after if/for/while/def/class

# Wrong if x > 10: # Nothing here - causes IndentationError

# Fix: Add pass or code if x > 10: pass

# Or add actual code if x > 10: print("Large")

# For functions def my_function(): # Missing body

def my_function(): pass # Placeholder

# For classes class MyClass: # Missing body

class MyClass: pass # Placeholder ```

Solution 5: Fix Copy-Paste Indentation

```python # Problem: Code copied from different source has wrong indentation

def process(): # Original code with 4-space indent data = load_data() # Copied code with 2-space indent for item in data: # Doesn't match function's indentation process_item(item)

# Fix: Re-indent copied code to match def process(): data = load_data() for item in data: # Now matches function's 4-space indent process_item(item)

# Use editor's format feature # VS Code: Shift+Alt+F # PyCharm: Code -> Reformat Code ```

Solution 6: Standardize Indentation in Project

```python # Create indentation config file # .editorconfig (works with many editors)

# .editorconfig content: """ root = true

[*.py] indent_style = space indent_size = 4 charset = utf-8 end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true """

# Or setup.cfg for flake8 """ [flake8] max-line-length = 88 indent-size = 4 """ ```

Solution 7: Use Auto-Formatting Tools

```bash # Install autopep8 pip install autopep8

# Fix indentation in file autopep8 --in-place --aggressive app.py

# Or use black (opinionated formatter) pip install black black app.py

# Use with pre-commit pip install pre-commit # .pre-commit-config.yaml: """ repos: - repo: https://github.com/psf/black rev: stable hooks: - id: black """ pre-commit install ```

Solution 8: Fix Nested Block Indentation

```python # Problem: Complex nesting with inconsistent indentation

def process(): if condition1: if condition2: for item in items: process_item(item) # Wrong indent level return result # Correct

# Fix: Use consistent indentation at each level # Standard: 4 spaces per level def process(): if condition1: # Level 1 if condition2: # Level 2 for item in items: # Level 3 process_item(item) # Level 4 return result # Level 2

# Visual guide for indentation levels: # 0 spaces: File level # 4 spaces: def/class/if/for/while body # 8 spaces: Nested block inside first level # 12 spaces: Third level nesting # etc. ```

Indentation Rules Reference

Basic Indentation Structure

```python # Level 0: Top-level code import os

x = 10

# Level 1: Inside def/class/if/for/while def function(): # Level 2: Inside function if condition: # Level 3: Inside if block for item in items: # Level 4: Inside for block pass

# Back to Level 2 return result

# Back to Level 0 print("Done") ```

Continuation Lines

```python # Long expressions can be continued with alignment result = very_long_function_name( first_argument, second_argument, third_argument )

# Or use hanging indent result = very_long_function_name( first_argument, second_argument, third_argument ) # Closing paren at original indent level

# Multiple conditions in if if (first_condition and second_condition and third_condition): do_something() ```

Multi-line Statements

```python # Lists, dicts can have any indentation items = [ 1, 2, 3, ]

data = { 'key1': 'value1', 'key2': 'value2', }

# But code blocks must follow strict indentation if condition: items = [ 1, # This must be indented from if 2, ] ```

Debugging Indentation

Visual Indentation Checker

```python def show_indent_structure(filename): """Show indentation structure of a Python file.""" with open(filename) as f: for i, line in enumerate(f, 1): if line.strip(): indent = len(line) - len(line.lstrip()) level = indent // 4 # Assuming 4-space indent bars = '|' * level content = line.strip()[:40] print(f"{i:3}: {bars} {content}") else: print(f"{i:3}: (empty)")

show_indent_structure("app.py") ```

Indentation Validator

```python def validate_indentation(filename, expected_spaces=4): """Validate consistent indentation.""" issues = []

with open(filename) as f: prev_indent = 0 for i, line in enumerate(f, 1): if line.strip(): indent = len(line) - len(line.lstrip())

# Check for tabs if '\t' in line[:indent]: issues.append((i, "Contains tabs"))

# Check for non-multiple of expected if indent % expected_spaces != 0: issues.append((i, f"Indent {indent} not multiple of {expected_spaces}"))

return issues

issues = validate_indentation("app.py") for line, msg in issues: print(f"Line {line}: {msg}") ```

Prevention Tips

  1. 1.Configure editor to use spaces (not tabs)
  2. 2.Set consistent tab size (4 spaces is standard)
  3. 3.Use auto-formatters (black, autopep8)
  4. 4.Enable visible whitespace in editor
  5. 5.Use pre-commit hooks for automatic formatting

```python # Recommended editor setup # - Tab key inserts 4 spaces # - Show invisible characters # - Auto-indent matches previous line # - Trim trailing whitespace on save # - Insert final newline on save

# VS Code settings """ { "editor.tabSize": 4, "editor.insertSpaces": true, "editor.formatOnSave": true, "editor.renderWhitespace": "boundary", "files.trimTrailingWhitespace": true, "files.insertFinalNewline": true } """ ```

  • TabError - Specifically for mixed tabs and spaces
  • SyntaxError - Other syntax issues
  • SyntaxError: expected an indented block - Empty block
  • SyntaxError: unindent does not match - Same as IndentationError