TabError: inconsistent use of tabs and spaces in indentation
Updated
The Python error "inconsistent use of tabs and spaces in indentation" means one block mixes tabs and spaces. How to see the tabs, and convert them.
What you see
def area(width, height):
result = width * height
return result
print(area(3, 4))The first line of the body starts with a tab, the second with eight spaces. They look the same in an editor that shows a tab as eight columns.
File "/home/me/project/main.py", line 3
return result
TabError: inconsistent use of tabs and spaces in indentationWhat it means
One block is indented partly with tabs and partly with spaces, in a way whose meaning depends on how wide a tab is. Python 3 refuses to guess and stops with TabError, a subclass of IndentationError.
Python 2 treated a tab as eight columns and accepted this code. Python 3 does not, which is why old code and code from mixed editors fail here.
Why it happens
- Two editors, two settings. One inserts tabs, the other spaces.
- Pasting code indented with tabs into a file indented with spaces.
- An editor that shows tabs as four or eight columns, so the mix is invisible.
How to fix it
Pick spaces, four per level, and convert the whole file. In VS Code, open the command palette and run "Convert Indentation to Spaces"; PyCharm has Edit › Convert Indents › To Spaces.
From a terminal, expand replaces tabs with spaces. Give it the tab width the code was written with:
expand -t 8 main.py > fixed.py
Then set your editor to insert spaces when you press Tab, so it does not come back.
How each Python version words it
| Python | Prints |
|---|---|
| 3.9–3.15 | TabError: inconsistent use of tabs and spaces in indentation |
Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on October 25, 2026.
The wording is the same on every version here.
Questions
How do I see which lines have tabs?
Turn on whitespace rendering in the editor. In a terminal, cat -et main.py on macOS or cat -A main.py on Linux prints each tab as ^I. python3 -m tabnanny main.py names the first ambiguous line.
Is a file indented only with tabs allowed?
Yes. Python accepts tabs as long as each block is consistent. PEP 8 recommends spaces, and most Python code uses them.
Sources
- Lexical analysis: indentation, Python language reference
- Built-in exceptions: TabError, Python documentation
- PEP 8: tabs or spaces?, Python Enhancement Proposals
Read on October 25, 2026.