TabError: inconsistent use of tabs and spaces in indentation (Python)

A close up photo of a typewriter and a paper with the word "vintage" written on it
Photo by Suzy Hazelwood, source: Pexels

🚫 TabError: inconsistent use of tabs and spaces in indentation

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

Here’s what it looks like:

File, line 4
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 there's a whitespace inconsistency in the respective indentation level.

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 editor), you might have to check the indentation.

A python block with is using tabs and spaces for indentation

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

Python disallows mixing spaces and tabs in the same indentation level - for instance, to indent the lines inside a for loop. Sometimes a mix of tab spaces can cause to other indentation errors, which makes the debugging even more confusing.

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

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 4 "\tprint('tab')"

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

How to fix the "unindent does not match any outer indentation level" error

To avoid this situation, you can make all whitespaces visible in your code editor. These indicators give you quick 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 enter ()

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

And to make whitespaces consistent, 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 (Ctrl + A) to see the whitespaces in your code.

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

And to make whitespace characters visible in Vim:

First, 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.

Author photo

Hey 👋 I'm a software engineer, an author, and an open-source contributor. I enjoy helping people (including myself) decode the complex side of technology. I share my findings on Twitter: @lavary_

If you read this far, you can tweet to the author to show them you care. Tweet a Thanks

In this article:

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