On "TabError: inconsistent use of tabs and spaces in indentation" in Python
Tags: python, programming
Published: 2023-02-04 14:17:59

> ✋ **Update:** This post was originally published on my blog [decodingweb.dev](https://www.decodingweb.dev), where you can read the [latest version](https://www.decodingweb.dev/python-taberror-inconsistent-use-of-tabs-and-spaces-in-indentation-fix) for a 💯 user experience. _~reza_

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

- <u>Visual Studio Code</u>
- <u>Sublime Text</u>
- <u>Vim </u>

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

- [TypeError: missing 1 required positional argument: 'self' (Fixed)](https://www.decodingweb.dev/python-typeerror-missing-1-required-positional-argument-self)
- [Unindent does not match any outer indentation level error in Python (Fixed)](https://www.decodingweb.dev/python-indentationerror-unindent-does-not-match-any-outer-indentation-level-fix)
- [AttributeError module 'DateTime' has no attribute 'strptime' (Fixed)](https://www.decodingweb.dev/attributeerror-module-datetime-has-no-attribute-strptime-python)
- [AttributeError: 'str' object has no attribute 'decode' (Fixed)](https://www.decodingweb.dev/python-attributeerror-str-object-has-no-attribute-decode-fix)
- [How back-end web frameworks work?](https://www.decodingweb.dev/backend-web-frameworks)