TabError: inconsistent use of tabs and spaces in indentation
TabError: inconsistent use of tabs and spaces in indentation in Python means the same block is indented with tabs on some lines and spaces on others. Python 3 refuses to guess how wide a tab is, so it stops.
How do I fix TabError: inconsistent use of tabs and spaces in indentation?
TabError: inconsistent use of tabs and spaces in indentation means one block is indented with tab characters on some lines and spaces on others. The code can look perfectly aligned. Convert the whole file to spaces with your editor's Convert indentation to spaces command, and set the Tab key to insert 4 spaces.
- 1. Make tabs visibleTurn on render whitespace. Tabs appear as arrows and spaces as dots, so mixed lines stand out.
- 2. Convert the whole fileUse Convert indentation to spaces on the entire file, not just the reported line. Pasted snippets often bring tabs.
- 3. Find lines from a terminalRun python -m tabnanny yourfile.py. It prints the lines whose indentation is ambiguous.
What does TabError: inconsistent use of tabs and spaces in indentation mean?
A tab and four spaces can look identical on screen, but they are different characters. Python 3 does not allow a block to depend on how wide your editor draws a tab, so mixing them in a way that changes the meaning is an error. The code looks fine, which is what makes this error confusing.
Why does TabError: inconsistent use of tabs and spaces in indentation happen?
1. Code copied from several places
One snippet used tabs, another used spaces. Pasted together, the block mixes both.
Breaks
def total(items):
s = 0
for x in items: # 8 spaces
s += x
return sWorks
def total(items):
s = 0
for x in items:
s += x
return s2. An editor set to insert real tabs
Some editors type a tab character when you press Tab. Lines you indented by hand with the spacebar then disagree.
Breaks
if ok:
print("yes") # Tab key
print("sure") # 8 spacesWorks
if ok:
print("yes")
print("sure")How to find it in your own code
Checklist: 1) Turn on "render whitespace" so tabs show as arrows. 2) Use the editor command "Convert indentation to spaces" on the whole file. 3) Set the Tab key to insert 4 spaces (the Python standard). 4) To find the bad lines from a terminal, run python -m tabnanny yourfile.py.
Still not sure why yours breaks?
Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.