python syntax_error ai_generated true

IndentationError: unexpected indent

ID: python/indentationerror

Also available as: JSON · Markdown
98%Fix Rate
95%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
311 active

Root Cause

Mixed tabs and spaces, or wrong indentation level.

generic

Workarounds

  1. 98% success Use consistent indentation: 4 spaces (PEP 8 standard)
    4 spaces (PEP 8 standard)

    Sources: https://peps.python.org/pep-0008/#indentation

  2. 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

  3. 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:

  1. Auto-format with a random formatter 55% fail

    Different formatters have different defaults — pick one and stick with it

Error Chain

Frequently confused with: