Last updated: Apr 8, 2024
Reading time·4 min
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).
To solve the error, remove the whitespace and only indent the lines in the code block using tabs or using spaces.
# ✅ Correctly indented code if len('hi') == 2: print('a') print('b')
Make sure the lines of code in the code block at indented to the same level.
You can either indent your code using tabs or using spaces.
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.
If you use Sublime Text, you can set it to use Tabs for indentation by:
You can also uncheck the Indent using spaces checkbox if you have it checked.
It is a matter of personal preference if you use only tabs or only spaces, but make sure not to mix the two.
# ✅ Consistently indented code def my_function(): if len('hi') == 2: print('a') print('b') elif len('one' == 1): print('c') else: print('d')
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.
autopep8
module to solve the errorThe autopep8 module automatically formats code to the PEP 8 style guide.
The module can be used to fix indentation errors.
You can install autopep8
by running the following command.
pip install --upgrade autopep8 # 👇️ Or with pip3 pip3 install --upgrade autopep8 # 👇️ In case you don't have PIP in PATH environment variable python -m pip install --upgrade autopep8 python3 -m pip install --upgrade autopep8 # 👇️ For Windows py -m pip install --upgrade autopep8
Now you can use the autopep8
command to solve the error.
autopep8 -i main.py
Make sure to save your file after running the autopep8
command.
You can use the tabnanny
built-in module to detect on which line the
indentation error occurred.
python -m tabnanny main.py # 👇️ For Python 3 python3 -m tabnanny main.py # 👇️ For Windows py -m tabnanny main.py
Make sure to replace main.py
with the name of the file that contains the
error.
The error message shows that the error occurred on line 3.
In other words, I have to remove the leading whitespace from line 3 and indent the code consistently (using only spaces or only tabs).
If you got the error in the IDLE code editor:
3. Leave 8 columns per tab selected and click OK.
After you click on the OK button, save your file and you should be good to go.
The pep8 style guide recommends using spaces over tabs for indentation.
According to the recommendation, tabs should be used in a code base that is already indented using tabs.
However, they recommend using spaces for new projects.
As previously noted, Python disallows mixing tabs and spaces for indentation.
Whether you use tabs or spaces is a matter of personal preference, but make sure to not mix the two in the same codebase as that often causes issues in Python.
You can learn more about the related topics by checking out the following tutorials: