Inconsistent use of tabs and spaces
Production Risk
Trivial to fix; configure editors to show whitespace and use spaces only.
A subclass of IndentationError raised when a file mixes tabs and spaces for indentation in a way that is ambiguous. Python 3 forbids mixing tabs and spaces in the same indented block.
- 1A file uses spaces in some lines and tabs in others within the same block
- 2Pasting code from different editors that use different indentation conventions
- 3Invisible tab characters in a file that looks space-indented
A file mixes tab and space indentation.
def foo():
x = 1 # 4 spaces
y = 2 # tab
return x + yexpected output
TabError: inconsistent use of tabs and spaces in indentation
Fix
Convert all indentation to spaces
WHEN Fixing a file with mixed indentation
# Use expand to convert tabs to spaces expand -t 4 mixed_file.py > fixed_file.py # Or use autopep8: autopep8 --indent-size 4 -i mixed_file.py # Or use your editor to show whitespace: # VS Code: View → Render Whitespace
Why this works
expand replaces tab characters with spaces; autopep8 reformats indentation consistently.
def foo():
x = 1 # spaces
y = 2 # tab — TabError: inconsistent use of tabs and spacestry:
compile(source, "<string>", "exec")
except TabError as e:
print(f"Mixed indentation at line {e.lineno}")# .editorconfig # [*.py] # indent_style = space # indent_size = 4 # Convert existing: expand -t 4 file.py > fixed.py
✕ Mix tabs and spaces — ever
Python 3 is strict about this; configure your editor to use spaces only (PEP 8 mandates 4 spaces).
Python Docs — Built-in Exceptions
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev