On this page
article
IndentationError: unexpected indent (variant 5)
How to fix IndentationError in Python — unexpected indent.
Error Message
IndentationError: unexpected indent
What It Means
Python raised IndentationError because something in your code violated language rules or runtime expectations.
Common Causes
- Typo in variable, function, or module name
- Wrong data type for the operation
- Missing import or uninstalled dependency
- Incorrect indentation or syntax
How to Fix
Use 4 spaces consistently; never mix tabs and spaces.
Example
# Problem scenario related to IndentationError
# ... code that triggers the error ...
# Fixed version
# Apply the fix described above
Prevention
- Run
python -m py_compile script.pybefore committing - Use a linter (
ruff,flake8) and type checker (mypy) - Write tests for edge cases (empty input, None, zero)