How to fix “unindent does not match any outer indentation level” indentation error in Python

Fix unindent does not match any outer indentation level

Python is an emerging language in the developer field. It’s great as first language because it’s concise, easy to read, fast enough for most tasks and has a large ecosystem which fits mostly everything from web development to scientific programming.

Back then when I was new to Python, one of the thing that frustrates me the most was IndentationError: unindent does not match any outer indentation level. Look at how it made people pull their hair out :

Fixing this error is easy, and should be easy, but to understand why it happens so that you can write clean, concise, maintainable code requires more reading.

What “unindent does not match any outer indentation level” means?

Just like the error message says, there could be something wrong with the spaces and tabs indentation in your code.

Along with the IndentationError provided, the terminal should give you the exact line the error is occuring on. Using that piece of information, you could look closely into where the problem arised.

What causes “unindent does not match any outer indentation level” in Python

There are a number of reasons for this error to happen. This is an exaustive list of main causes, sorted by popularity :

  1. You are mixing tabs and spaces in your code. There might be a few spaces mixed in with your tabs, or vice versa. If you are using a text editor, you can manually doing a search and replace with tabs and spaces. However, using a source-code editor or a full-fledged IDE is a better idea since they have automatic correction built-in.
  2. You may have placed a few extra spaces or tabs here and there. For example, you could have put 5 spaces where it should be 4 spaces at the beginning of a line.
  3. You forgot to indent compound statements, which could be any ‘if’, ‘for’, ‘while’ etc.
  4. User-defined functions/classes was not indented properly.

Manually fix IndentationError

If you are using some kind of primitive editors like Notepad or Gedit, you should try doing an exhaustive search and replace like this

  • Press Ctrl + H to open Find and Replace, then replace any tab with exactly 4 spaces
  • Find and Replace again, this time replace exactly 5 spaces with 4 spaces to remove any extra spaces left in the code.
  • Your code should run without errors by now, but if it doesn’t, I highly suggest switching to another editor. My recommendation is Visual Studio Code.

Format your code properly to avoid “unindent does not match any outer indentation level”

Solution 1 : Using Visual Studio Code

First, you need to have Visual Code Studio installed

Open your source code in Visual Studio Code, then press one of these shortcuts to run Code Formatting:

  • Visual Studio Code on Windows – Shift + Alt + F
  • Visual Studio Code on MacOS – Shift + Option + F
  • Visual Studio Code on Ubuntu – Ctrl + Shift + I

Even better, you can also edit the preferences to enable Visual Studio Code‘s automatic code formatting right before saving any .py files. To do that, open File → Preferences → Workspace Settings (or alternatively press Ctrl + , ) and add all the config below to workspace settings:

{ "editor.formatOnSave": true }

Note : to properly format your code with all the recommendations in PEP 8 — Style Guide for Python Code, you should install autopep8, which fixes numerous styling problems. I personally got rid of IndentationError messages since I used it. Also, automatic code linting with flake8 or pylint is something a professional developer can’t live without, so check it out if you want to level up your game a little bit.

Solution 2 : Using Python’s IDLE

For those of you who don’t know what IDLE is, IDLE stands for Integrated Development Environment, and it’s bundled in all versions of Python.

Python’s IDLE have its own way of fixing this, just follow the steps below, if it suggests one similar message:

  1. Choose All, e.g. Ctrl + A
  2. Select Format → Untabify Region
  3. Double check if your indenting is still correct, then save and rerun the program.

Solution 3 : Using Atom

For Atom users, follow these steps to reformat the code:

  1. Go to Packages.
  2. Choose Whitespace.
  3. Convert Spaces to Tabs.
  4. Check your file indentation.