Unexpected indent

Python SystemParser ErrorCommonLast updated: June 29, 2026Tested on:CPython v3.12PIP v24.0June 2026

This error occurs when a line of code is indented more than the active block requires, violating Python's block definition margins.

Unexpected indent Quick Fix⏱️ Est. Fix Time: 1 minute

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).
CauseFrequency
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

1Re-align to baseline columns: Move the offending line back to align exactly with surrounding statements.
2Use space normalize formatting tools: Re-save files using code formatting tools (like Black) that enforce standard PEP 8 spacing margins.
3Show invisible whitespace: Enable whitespace characters visibility inside the text editor to trace hidden spacing discrepancies.

Python Operations & Verification

Align variables and standard statements to the same base column margin.

Re-aligning Margins Example
# Wrong: y is unexpectedly indented
# x = 5
#   y = 10

# Correct
x = 5
y = 10

Platform Specific Fixes

Examine invisible spaces and tabs using cat formatting switches.

Linux Config
# Shows tabs as ^I and line ends as $
cat -A script.py

Best 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'.

Still having this problem?

Didn't solve your problem?

SuggestRequest Error