How To Fix IndentationError: Unindent Does Not Match Any Outer Indentation Level Error In Python

When I run my python script code, I meet an error like this IndentationError: unindent does not match any outer indentation level. This error is so strange and hard to find the reason. After some investigation, I finally find the solution.

1. Why IndentationError.

Indentationerror error is always because of tab and white space mixed in your python code. We generally use 4 white spaces to present indentation in python. But as a coder, we may sometimes use the tab key to present the indentation. So if you mix used white space and tab for indentation, this error may occur.

2. How To Locate Indentation Error.

When you run your python code, if the code has an indentation error, it will throw the error message out to you which will make you struggled. The error message will tell you which line of code has an indentation error.

C:\WorkSpace>test_sys_arg.py
  File "C:\WorkSpace\test_sys_arg.py", line 11
    print('argv ' + str(i) + ' = ' + tmp_argv)
                                             ^
IndentationError: unindent does not match any outer indentation level

But you can also run the python command python -m tabnanny test_sys_arg.py to know which line of code has an indentation error.

C:\WorkSpace>python -m tabnanny test_sys_arg.py
'test_sys_arg.py': Indentation Error: unindent does not match any outer indentation level (<tokenize>, line 11)

3. How To Fix Python IndentationError.

We all know one tab contains four white spaces, so we need to know where we use four white spaces to replace one tab in python source code. Please follow the below steps.

  1. Open your python source file use one text editor like Sublime Text.
  2. Then press Ctrl + A to select all python source code in the editor, then you can see at the beginning of line 11, there are four white spaces ( four dots) that should be replaced with one tab.
    see-python-indentation-use-tab-or-white-space-in-sublime-text
  3. Replace the four white space with one tab at the beginning of line 11, save the file and run it again, you will find the error has been fixed.
  4. There is also a shortcut in Sublime text which can help you to make conversion between tab to spaces.
  5. Click the View —> Indentation menu item in Sublime text, then it will list some sub-menu items.
  6. Click the Convert Indentation to Spaces or Convert Indentation to Tabs menu item can do the tab and spaces conversion.
  7. You can also specify to use white spaces for indentation by checking the Indent Using Spaces submenu item.
  8. Or you can select how many spaces that one tab will contain by choosing different Tab Width values.

4 thoughts on “How To Fix IndentationError: Unindent Does Not Match Any Outer Indentation Level Error In Python”

  1. I use notepad++ to edit my python script, and it also displays the error IdentationError : unindent does not match any outer indentation level. And I found this article is helpful to fix my error, but can any body tell me how to convert all the tab to 4 or 8 white spaces in notepad++, this can make the error resolving more efficiency. Thanks.

    1. You can try the below steps in notepad++ to convert all tab key to white spaces.

      Press Ctrl + A to select all the python souce code.
      Press the Tab key, it will add tab indentation everywhere in your python source code.
      Press Shift + Tab key together, it will use 4 white spaces to replace all the tab characters in the python source code.
      You can change the notepadd++ settings by click the Settings —> Preferences —> Tab Settings menu item, then select Replace by spaces sub menu item, then it will always use 4 white spaces to replace the tab character when you press tab in notepad++.

  2. Kazi Imam Hasan

    For visual studio code, go to editor settings then dis-select the box ‘insert spaces when pressing tab’. The problem will be solved.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.