I'm getting an IndentationError. How do I fix it?

Viewed 59523

I have a Python script:

if True:
    if False:
        print('foo')
   print('bar')

However, when I attempt to run my script, Python raises an IndentationError:

  File "script.py", line 4
    print('bar')
               ^
IndentationError: unindent does not match any outer indentation level

I kept playing around with my program, and I was able to produce four errors in total:

  • IndentationError: unexpected indent
  • IndentationError: expected an indented block
  • TabError: inconsistent use of tabs and spaces in indentation
  • IndentationError: unindent does not match any outer indentation level

What do these errors mean? What am I doing wrong? How can I fix my code?


Note: This is an attempt at a canonical question because I see many similar posts every month. This is not a duplicate of existing questions about unindents or unexpected indents because they only deal with one type of indentation error each, and I'm looking to cover them all in one place.

It's also possible to have logically incorrect indentation that does not cause an error message. One common form of this is attaching else: to a for or while loop rather than (as intended) the corresponding if:. See Else clause on Python while statement if you need to close questions where OP did that.

5 Answers

Sublime Text 3

If it happens that you code in Sublime Text 3, this could help you with indentations problemes

In Sublime Text, while editing a Python file:

Sublime Text menu > Preferences > Settings - Syntax Specific :

Python.sublime-settings

{
    "tab_size": 4,
    "translate_tabs_to_spaces": true
}

You see, you have a tiny error.

if True:
    if False:
        print('foo')
   print('bar')

You were supposed to do:

if True:
    if False:
        print('foo')
    print('bar')

As you can see your print is only indented 3 spaces, it is supposed to be indented 4 spaces.

Historical note for Python 2

By default, Python 2 allowed tabs and spaces to be mixed, and would not produce an error unless the -t or -tt options are passed to Python. The full details are explained at Python's interpretation of tabs and spaces to indent.

Note in particular that tabs are treated as 8 spaces (rather, they effectively increase the number of perceived spaces up to the next multiple of 8). Thus, if you were displaying the code with a standard 4-space indent, but have mixed spaces and tabs, you could end up with code that satisfies the indentation rules, but is not considered to be indented the same way that it looks.

As a result, you could get all kinds of other errors. For example:

# in 2.x
def example():
    def nested(): # suppose this is indented with a tab
        x = [1] # and this with two tabs
        print x[0] + 1 # but this with 8 spaces instead of a tab
    nested()

(Note that Stack Overflow's Markdown rendering will show the indentation as spaces even if I use tabs.)

That gives a NameError, since print x is no longer inside the nested function and x is out of scope in the outer example. Similarly, we could easily create a TypeError by giving example a local x = 1, or ValueError by giving it a local x = [].

Quick fix for Sublime users:

  1. Press Ctrl-H to access Find and Replace
  2. In Find: Type 4 spaces
  3. In Replace: Copy and paste a tab from somewhere in your code Click Replace All
Related