TabError: inconsistent use of tabs and spaces in indentation - Python

In this short post you can find how to fix error: TabError: inconsistent use of tabs and spaces in indentation.

The reason for the error is mixing tabs and spaces. The code below illustrates the error:

for n in range(0, 5):
    print(n)
	print(1)

You can use text editor like Sublime to investigate the error:

Reason for the error

Which one should we use: Tabs or Spaces?. Python PEP8 recommeds using spaces. But if you use only tabs - that's fine too.

Spaces are the preferred indentation method.

Tabs should be used solely to remain consistent with code that is already indented with tabs.

Python disallows mixing tabs and spaces for indentation.

Fix in Sublime

To fix error: TabError: inconsistent use of tabs and spaces in indentation in Sublime do:

  • View
  • Indentation
  • Convert Indentation to Spaces

The error is fixed now.

VSCode

To solve this error in Visual studio code you can do:

  • CTRL + Shift + P
  • Type Convert Indentation to Spaces
  • Enter

This option is available when you select Python file.

Atom

  • Packages
  • WhiteSpace
  • Convert all Tabs to Spaces

Black and Autopep8

We can use pacgakes to analyse the project for this error like:

autopep8 -i temp.py
black temp.py