IndentationError: What It Means and How to Fix It
Error reference
IndentationError
Language
Severity
HighWhen it happens
An IndentationError is a specific form of SyntaxError raised in Python when the indentation of a block of code does not follow the expected pattern.
Potential fixes
Configure your editor to use spaces only (4 spaces per indent level is the Python convention). Enable 'show whitespace' in your editor to spot mixed tabs and spaces. Run your linter or formatter (such as ruff or black) to auto-fix indentation.
Related errors
Deep dive
An IndentationError is a specific form of SyntaxError raised in Python when the indentation of a block of code does not follow the expected pattern. Python uses indentation to define code blocks, so inconsistent spaces or tabs cause this error.
Why it happens
- •Mixing tabs and spaces for indentation in the same file.
- •Using a different number of spaces for a line that should be at the same level.
- •Accidentally removing or adding a space when editing code.
- •Copying code from another source that uses a different indentation style.
Potential fixes
Configure your editor to use spaces only (4 spaces per indent level is the Python convention). Enable 'show whitespace' in your editor to spot mixed tabs and spaces. Run your linter or formatter (such as ruff or black) to auto-fix indentation.
Examples
Code that triggers the error
def calculate(x):
result = x * 2
return resultError output
File "app.py", line 3
return result
IndentationError: unindent does not match any outer indentation levelFixed code
def calculate(x):
result = x * 2
return result
print(calculate(5)) # 10Practice in English
How would you explain an IndentationError to a fellow dev? Choose the right phrase:
"I hit an error...
Ready to practice your English at work?
Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.
Try Lingua-e for free