Last updated: Apr 8, 2024
Reading timeยท5 min
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.
Here is an example of how the error occurs.
# โ๏ธ 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.
The screenshot shows that the print('a')
line was indented using tabs, and the
print('b')
line was indented using spaces.
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.
You should only use tabs in the entire code block or only use spaces (depending on your preference).
The best way to solve the error is to remove the leading whitespace and indent the lines only using tabs.
# โ Correctly indented code if len('hi') == 2: print('a') elif len('hello') == 5: print('b') else: print('c')
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.
If you use VSCode, you can solve the error by using the "Convert indentation to spaces" or "Convert indentation to tabs" commands:
CTRL + Shift + P
or (โ
+ Shift
+ P
on Mac) to open the command
palette.If you use VSCode, you can show whitespace characters by:
CTRL + Shift + P
or (โ
+ Shift
+ P
on Mac) to open the
command palette.renderwhitespace
all
If you render whitespace characters in your IDE, tabs should show as arrows and spaces should show as dots.
If you use Sublime Text, you can set it to use Tabs for indentation by:
You can also uncheck the Indent using spaces checkbox if you have it checked.
If you use VIM, hit Escape
, type gg=G
and press Enter
.
Notice how the line got automatically indented correctly.
If you use the IDLE code editor:
CTRL
+ A
to select all of the code in your file.3. Leave 8 columns per tab selected and click OK.
After you click on the OK button, save your file and you should be good to go.
It is a matter of personal preference if you use only tabs or only spaces, but make sure not to mix the two.
The error is only raised if you mix tabs and spaces in the same code block.
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.
You can use the tabnanny
built-in module to detect on which line the
indentation error occurred.
python -m tabnanny main.py # ๐๏ธ For Python 3 python3 -m tabnanny main.py # ๐๏ธ For Windows py -m tabnanny main.py
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 the indentation level of your code to determine which code belongs to a block.
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.
When a line ends with a colon, the next line should be indented to be considered a part of the block.
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.
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 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.