python
syntax_error
ai_generated
true
IndentationError: unexpected indent
ID: python/indentationerror
98%Fix Rate
95%Confidence
50Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 311 | active | — | — | — |
Root Cause
Mixed tabs and spaces, or wrong indentation level.
genericWorkarounds
-
98% success Use consistent indentation: 4 spaces (PEP 8 standard)
4 spaces (PEP 8 standard)
-
95% success Configure your editor to convert tabs to spaces
# VS Code settings.json: # "editor.insertSpaces": true, "editor.tabSize": 4 # Or add .editorconfig: # [*.py] # indent_style = space # indent_size = 4
Sources: https://docs.python.org/3/reference/lexical_analysis.html#indentation
-
85% success Run: python -tt script.py to check for mixed tabs/spaces
python -tt script.py to check for mixed tabs/spaces
Sources: https://docs.python.org/3/using/cmdline.html#cmdoption-tt
Dead Ends
Common approaches that don't work:
-
Auto-format with a random formatter
55% fail
Different formatters have different defaults — pick one and stick with it
Error Chain
Frequently confused with: