Python: inconsistent use of tabs and spaces in indentation

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# Python: inconsistent use of tabs and spaces in indentation

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.

taberror inconsistent use of tabs and spaces in indentation

Here is an example of how the error occurs.

main.py
# ⛔️ 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.

mixing tabs and spaces

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).

# Only indent the lines using tabs or spaces

To solve the error, remove the whitespace and only indent the lines in the code block using tabs or using spaces.

main.py
# ✅ Correctly indented code if len('hi') == 2: print('a') print('b')

correctly indent your code

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.

However, you shouldn't mix tabs and spaces in the same code block as that often causes issues in Python.

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.

taberror inconsistent use of tabs and spaces in indentation

# Convert indentation to spaces or tabs

If you use VSCode, you can solve the error by using the "Convert indentation to spaces" or "Convert indentation to tabs" commands:

  1. press CTRL + Shift + P or ( + Shift + P on Mac) to open the command palette.
  2. type: "convert indentation to"
  3. Select your preferred option
  4. Save the file

vscode convert indentation to

# Showing the whitespace characters in your IDE

If you use VSCode, you can show whitespace characters by:

  1. pressing CTRL + Shift + P or ( + Shift + P on Mac) to open the command palette.
  2. typing "open workspace settings"
  3. typing renderwhitespace
  4. setting it to all

vscode render whitespace

If you render whitespace characters in your IDE, tabs should show as arrows and spaces should show as dots.

mixing tabs and spaces

If you use Sublime Text, you can set it to use Tabs for indentation by:

  • Clicking on View -> Indentation -> Convert indentation to Tabs

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.

main.py
# ✅ Consistently indented code def my_function(): if len('hi') == 2: print('a') print('b') elif len('one' == 1): print('c') else: print('d')
Note that if you use tabs in one code block and spaces in the other, you won't get an error.

The error is only raised if you mix tabs and spaces in the same code block.

only using tabs or spaces

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.

# Using the autopep8 module to solve the error

The 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.

shell
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.

shell
autopep8 -i main.py

using autopep 8 to solve the error

Make sure to save your file after running the autopep8 command.

# Using tabnanny to detect indentation errors

You can use the tabnanny built-in module to detect on which line the indentation error occurred.

shell
python -m tabnanny main.py # 👇️ For Python 3 python3 -m tabnanny main.py # 👇️ For Windows py -m tabnanny main.py

using tabnanny to detect inconsistencies

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).

# Solving the error in the IDLE code editor

If you got the error in the IDLE code editor:

  1. Click on Edit in the top menu and then click Select all.
  2. Click on Format in the top menu and then click Untabify Region.

untabify region 3. Leave 8 columns per tab selected and click OK.

solve the error in idle ide

After you click on the OK button, save your file and you should be good to go.

# The pep8 style guide recommends using spaces

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.