How to fix “TabError: inconsistent use of tabs and spaces in indentation” in Python

Updated Sep 22, 2023 ⤳ 4 min read

The Python error “TabError: inconsistent use of tabs and spaces in indentation” occurs when you mix tabs and spaces to indent lines in a code block.

🎧 Debugging Jam

Calling all coders in need of a rhythm boost! Tune in to our 24/7 Lofi Coding Radio on YouTube, and let's code to the beat – subscribe for the ultimate coding groove!" Let the bug-hunting begin! 🎵💻🚀

Here’s what the error message looks like:


File test.py, line 4
  print('tab')
        ^
TabError: inconsistent use of tabs and spaces in indentation

Sometimes the lines look perfectly aligned, but you still get the error. If that happens, chances are some lines are indented with spaces and some with tabs.

You usually won't have to worry about mixing spaces and tabs because most modern editors automatically convert tabs to spaces as your write code. However, if you copy a piece of code from the Internet (or another text editor), you might have to double check the indentation.

In the above screenshot, VS code displays spaces as dots and tabs as tiny arrows.

Although tabs and spaces are interchangeable, the Python style guide (PEP 8) recommends using spaces over tabs (4 space characters per indentation level).

Python disallows mixing spaces and tabs in the same indentation level. Sometimes a mix of tab spaces can cause other indentation errors, which makes the debugging trickier.

According to PEP 8, if you're working with a code that's already using tabs, you can continue using them to keep the indentation consistent. 

How to quickly detect unwanted tabs

To detect ambiguous indentation errors, you can use the tabnanny module:


dwd@dwd-sandbox:~$ python -m tabnanny test.py
test.py 4 "\tprint('tab')"

In the above tabnanny output, line 4 is indented by a tab (\t).

How to deal with inconsistencies in indentation?

To avoid this tab error, you can make all whitespaces visible in your code editor. These indicators give you instant feedback as you write code.

Additionally, you can automatically turn all unwanted tabs into spaces without re-indenting each line manually.

Here's how to do it with three popular code editors:

  1. Visual Studio Code
  2. Sublime Text
  3. Vim 

Visual Studio Code: To make whitespace characters (space or tab) visible in VS code, press ⌘+Shift+P (on Mac) or Ctrl+Shift+P (on Windows) to open up the command palette. Then, type Toggle Render Whitespaces and hit return ()

As a result, VS Code will display space characters as gray dots and tabs as tiny arrows. 

And to make indentations consistent, while in the command palette, run Convert Indentation to Spaces or Convert Indentation to Tabs accordingly.

Sublime Text: If you have a space/tab indentation issue on Sublime Text, go to View ➝ Indentation and select Indent Using Spaces.

You can also highlight your code (by pressing Ctrl + A) to see the whitespaces.

Vim:  In Vim, you can use the :retab command to convert tabs into spaces automatically. 

And to make whitespace characters visible, run the following Vim command:


:set list

And then run:


:set listchars=space:,tab:-> 

You can replace and ➝ with the characters of your choice.

Alright, I think that does it! I hope you found this quick guide helpful.

Thanks for reading.

Disclaimer: This post may contain affiliate links. I might receive a commission if a purchase is made. However, it doesn’t change the cost you’ll pay.

`
Exit mobile version