Borislav Hadzhiev
Last updated: Apr 30, 2022
Check out my new book
The Python "TabError: inconsistent use of tabs and spaces in indentation" occurs when we mix tabs and spaces in the same code block. To solve the error, remove the spacing and only use tabs or spaces, but don't mix the two in the same code block.
Here is an example of how the error occurs.
# ⛔️ TabError: inconsistent use of tabs and spaces in indentation if len('hi') == 2: print('a') print('b')
The first line in the code block was indented using tabs, and the second - using spaces and tabs.
The screenshot shows that the print('a')
line was indented using tabs
(arrows), and the print('b')
line was indented using spaces and tabs (dots and
arrows).
Make sure the lines of code in the code block at indented to the same level.
Your error message should show the exact location where the error is raised, so you can remove the whitespace and consistently indent the lines in the code block using tabs or spaces.
If you use VSCode, you can solve the error by using the "Convert indentation to spaces" or "Convert indentation to tabs" commands:
CTRL + Shift + P
or (⌘
+ Shift
+ P
on Mac) to open the command
palette.If you use VSCode, you can show whitespace characters by:
CTRL + Shift + P
or (⌘
+ Shift
+ P
on Mac) to open the
command palette.renderwhitespace
all
If you render whitespace characters in your IDE, tabs should show as arrows and spaces should show as dots.
It is a matter of personal preference if you use only tabs or only spaces, but make sure not to mix the two.
The error is only raised if you mix tabs and spaces in the same code block.
The upper if
statement uses tabs for indentation, and the lower uses spaces.
The example doesn't cause an error because we didn't mix tabs and spaces in the same code block.