Unexpected indent
This error occurs when a line of code is indented more than the active block requires, violating Python's block definition margins.
Usually happens because:
- ☑ Accidental spaces typed at the beginning of a line
- ☑ Copy-pasting code block structures from external browsers
- ☑ Mixing space margins depths inside the same function block
🔍 Quick Checklist:
What is Unexpected indent?
The 'Unexpected indent' error is raised by the 'IndentationError' class when Python encounters a line of code that is pushed forward in space without a starting block header (like if, for, while, def, class). In Python, all code at the same hierarchy level must start at the exact same column position; adding accidental leading spaces or mixing tabs and spaces will confuse the parser, raising this error.
Common Causes
- Accidental leading spaces: Double-tapping space keys or copying code snippets with trailing indentation structures.
- Indenting standard statements: Pushing forward standard variables declarations or function calls that do not sit inside conditional loops.
- Mismatched nested blocks formatting: Writing nested structures with inconsistent space sizing depths (e.g. 2 spaces vs 4 spaces).
| Cause | Frequency |
|---|---|
| Accidental space inputs on standard statements | ⭐⭐⭐⭐⭐ |
| Copying and pasting block snippets from external browsers | ⭐⭐⭐⭐ |
| Mismatched indentation spacing depth (2 vs 4 spaces) | ⭐⭐⭐ |
Common Mistakes
- Copying snippets from PDF books or web browser blogs where the formatting engine introduces non-standard indent keys.
- Forgetting to verify whitespace rules in code loops where block statements mix tabs and spaces.
How to Fix
Python Operations & Verification
Align variables and standard statements to the same base column margin.
# Wrong: y is unexpectedly indented
# x = 5
# y = 10
# Correct
x = 5
y = 10Platform Specific Fixes
Examine invisible spaces and tabs using cat formatting switches.
# Shows tabs as ^I and line ends as $
cat -A script.pyBest Practices
- Configure text editors to display formatting characters automatically.
- Integrate code formatting pre-commit hooks (like pre-commit black) to normalize margins before commit.
Frequently Asked Questions (FAQ)
Q: What is 'unexpected indent'?
It means the parser found a line of code that is indented further than the surrounding code, but there is no block header (like 'if' or 'def') preceding it to justify the indentation.
Q: How do I find where the unexpected indent is?
The traceback prints a caret (^) pointing to the exact character where the indentation started. Move that line back to match the alignment of the lines around it.
Q: Why did copying code cause this error?
Code formatting can get scrambled when copied from web browsers. Select the block and press 'Shift+Tab' in your editor to un-indent, then re-align it using the Tab key or standard formatting shortcuts.
Q: How do I convert tabs to spaces?
In VS Code, open the Command Palette (Ctrl+Shift+P) and select 'Convert Indentation to Spaces'.