TabError
PythonERRORCommonSyntax ErrorHIGH confidence

Inconsistent use of tabs and spaces

Production Risk

Trivial to fix; configure editors to show whitespace and use spaces only.

What this means

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.

Why it happens
  1. 1A file uses spaces in some lines and tabs in others within the same block
  2. 2Pasting code from different editors that use different indentation conventions
  3. 3Invisible tab characters in a file that looks space-indented
How to reproduce

A file mixes tab and space indentation.

trigger — this will error
trigger — this will error
def foo():
    x = 1   # 4 spaces
	y = 2   # tab
    return x + y

expected output

TabError: inconsistent use of tabs and spaces in indentation

Fix

Convert all indentation to spaces

WHEN Fixing a file with mixed indentation

Convert all indentation to spaces
# 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.

Code examples
Triggerpython
def foo():
    x = 1   # spaces
	y = 2   # tab — TabError: inconsistent use of tabs and spaces
Handle at parse timepython
try:
    compile(source, "<string>", "exec")
except TabError as e:
    print(f"Mixed indentation at line {e.lineno}")
Avoid with editor configpython
# .editorconfig
# [*.py]
# indent_style = space
# indent_size = 4
# Convert existing: expand -t 4 file.py > fixed.py
What not to do

Mix tabs and spaces — ever

Python 3 is strict about this; configure your editor to use spaces only (PEP 8 mandates 4 spaces).

Sources
Official documentation ↗

Python Docs — Built-in Exceptions

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Python errors