Unindent does not match any outer indentation level (Python)

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
5 min

banner

# Unindent does not match any outer indentation level (Python)

The Python "IndentationError: unindent does not match any outer indentation level" occurs when we mix tabs and spaces in the same code block.

To solve the error, remove the spacing and only use tabs or spaces, but don't mix the two in the same code block.

indentationerror unindent does not match any outer indentation level

Here is an example of how the error occurs.

main.py
# โ›”๏ธ IndentationError: unindent does not match any outer indentation level if len('hi') == 2: print('a') # ๐Ÿ‘ˆ๏ธ Indented using tabs print('b') # ๐Ÿ‘ˆ๏ธ Indented using spaces

The first line in the code block was indented using tabs, and the second using spaces.

mixing tabs and spaces

The screenshot shows that the print('a') line was indented using tabs, and the print('b') line was indented using spaces.

# Ident all lines in the code block using tabs

To solve the error, remove the whitespace and only indent the lines in the code block using tabs or using spaces.

Your error message should show the exact location where the error is raised, so you can remove the whitespace and consistently indent the lines in the code block using tabs or spaces.

indentationerror unindent does not match any outer indentation level

You should only use tabs in the entire code block or only use spaces (depending on your preference).

However, you can't have a code block that was indented using both tabs and spaces.

The best way to solve the error is to remove the leading whitespace and indent the lines only using tabs.

main.py
# โœ… Correctly indented code if len('hi') == 2: print('a') elif len('hello') == 5: print('b') else: print('c')

correctly indent your code

If you copied the code from the internet and pasted it in your IDE, remove the indentation and manually indent the lines using only tabs.

# Convert indentation to tabs in your IDE

If you use VSCode, you can solve the error by using the "Convert indentation to spaces" or "Convert indentation to tabs" commands:

  1. press CTRL + Shift + P or (โŒ˜ + Shift + P on Mac) to open the command palette.
  2. type: "convert indentation to"
  3. Select your preferred option
  4. Save the file

vscode convert indentation to

If you use VSCode, you can show whitespace characters by:

  1. pressing CTRL + Shift + P or (โŒ˜ + Shift + P on Mac) to open the command palette.
  2. typing "open workspace settings"
  3. typing renderwhitespace
  4. setting it to all

vscode render whitespace

If you render whitespace characters in your IDE, tabs should show as arrows and spaces should show as dots.

mixing tabs and spaces

# Solve the error in Sublime Text

If you use Sublime Text, you can set it to use Tabs for indentation by:

  • Clicking on View -> Indentation -> Convert indentation to Tabs

You can also uncheck the Indent using spaces checkbox if you have it checked.

# Solve the error in VIM

If you use VIM, hit Escape, type gg=G and press Enter.

solve the error in vim

Notice how the line got automatically indented correctly.

# Solve the error in IDLE code editor

If you use the IDLE code editor:

  1. Press CTRL + A to select all of the code in your file.
  2. Click on Format in the top menu and then click Untabify Region.

untabify region 3. Leave 8 columns per tab selected and click OK.

fix indentation in idle

After you click on the OK button, save your file and you should be good to go.

# Make sure to not mix tabs and spaces

It is a matter of personal preference if you use only tabs or only spaces, but make sure not to mix the two.

Note that if you use tabs in one code block and spaces in the other, you won't get an error.

The error is only raised if you mix tabs and spaces in the same code block.

only using tabs or spaces

The upper if statement uses tabs for indentation and the lower uses spaces.

The example doesn't cause an error because we didn't mix tabs and spaces in the same code block.

# Using tabnanny to detect indentation errors

You can use the tabnanny built-in module to detect on which line the indentation error occurred.

shell
python -m tabnanny main.py # ๐Ÿ‘‡๏ธ For Python 3 python3 -m tabnanny main.py # ๐Ÿ‘‡๏ธ For Windows py -m tabnanny main.py

using tabnanny to detect indentation errors

Make sure to replace main.py with the name of your actual file in the command.

The error message shows that the error occurred on line 36.

In other words, I have to remove the leading whitespace from line 36 and indent the code consistently (using only spaces or only tabs).

# Python uses indentation to determine which code belongs to a block

Python uses the indentation level of your code to determine which code belongs to a block.

main.py
a_list = ['a', 'b', 'c'] for item in a_list: print(item) # ๐Ÿ‘ˆ๏ธ Inside the block print('last') # ๐Ÿ‘ˆ๏ธ Outside the block

The print(item) call in the for loop is indented and belongs to the block of the for loop.

The print call on the last line is not indented and doesn't belong to the block of the for loop.

The only way for Python to know if you meant to run the print() on each iteration of the for loop is based on the indentation level.

All of the lines in the code block have to be indented using tabs or spaces, but not both.

You shouldn't indent 1 line in a code block using tabs and the other using spaces as that causes the error.

# A code block is defined by ending a line with a colon

When a line ends with a colon, the next line should be indented to be considered a part of the block.

main.py
if len('hi') == 2: print('a') elif len('hello') == 5: print('b') else: print('c')

The line of the if, elif and else statements end with a colon, so to run code that is part of the corresponding block we have to indent it using tabs or spaces.

Make sure to not mix tabs and spaces, e.g. don't use tabs to indent the if block and spaces to indent the elif block.

Here are some examples of indenting code blocks after statements that end with a colon using tabs.

main.py
if len('hi') == 2: print('a') elif len('hello') == 5: print('b') else: print('c') def get_num(): return 100 for item in ['a', 'b', 'c']: print(item)

All of the code blocks are indented consistently using tabs, so everything works as expected.

# The pep8 style guide recommends using spaces

The pep8 style guide recommends using spaces over tabs for indentation.

According to the recommendation, tabs should be used in a code base that is already indented using tabs.

However, they recommend using spaces for new projects.

As previously noted, Python disallows mixing tabs and spaces for indentation.

Whether you use tabs or spaces is a matter of personal preference, but make sure to not mix the two in the same codebase as that often causes issues in Python.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev