On "TabError: inconsistent use of tabs and spaces in indentation" in Python

On "TabError: inconsistent use of tabs and spaces in indentation" in Python

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

Update: This post was originally published on my blog decodingweb.dev, where you can read the latest version for a 💯 user experience. ~reza

Here’s what it 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 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.

⚠️ Python disallows mixing spaces and tabs in the same indentation level - for instance, to indent the lines inside a for loop.

Although tabs and spaces are interchangeable, the Python style guide (PEP 8) recommends using spaces over tabs - four 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.

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 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:

  • Visual Studio Code

  • Sublime Text

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


❤️ You might like: