Python Error
IndentationError
IndentationError is a subclass of SyntaxError raised when Python encounters incorrect indentation. Since Python uses indentation to define code blocks, inconsistent or missing indentation prevents the code from being parsed.
Common causes
- Mixing tabs and spaces for indentation
- Missing indentation after colon (if, for, def, class)
- Extra or unexpected indentation
- Inconsistent indentation levels within a block
- Copy-pasting code from different sources with mixed whitespace
How to fix it
- Configure your editor to use spaces only (4 spaces per level)
- Use "Show Whitespace" in your editor to visualize tabs vs spaces
- Run python -tt script.py to detect tab/space mixing
- Use autopep8 or black to auto-format indentation
- Re-indent the affected block consistently
Example
IndentationError example
# Error example
def greet(name):
print(f"Hello, {name}") # IndentationError: expected an indented block
if True:
print("yes")
print("maybe") # IndentationError: unexpected indent
# Fix: consistent 4-space indentation
def greet(name):
print(f"Hello, {name}")
if True:
print("yes")
print("maybe") Track IndentationError with Checkend
Checkend automatically captures IndentationError errors in your Python application with full context:
- Complete traceback with syntax highlighting
- Request context (URL, params, headers)
- Automatic grouping of similar errors
- Instant notifications when errors occur
Related errors
Stop debugging in production
Get full error context and fix issues faster with self-hosted error tracking.