80
if len(trashed_files) == 0 :
    print "No files trashed from current dir ('%s')" % os.path.realpath(os.curdir)
else :
    index=raw_input("What file to restore [0..%d]: " % (len(trashed_files)-1))
    if index == "*" :
        for tfile in trashed_files :
            try:
                tfile.restore()
            except IOError, e:
                import sys
                print >> sys.stderr, str(e)
                sys.exit(1)
    elif index == "" :
        print "Exiting"
    else :
        index = int(index)
        try:
            trashed_files[index].restore()
        except IOError, e:
            import sys
            print >> sys.stderr, str(e)
            sys.exit(1)

I am getting:

        elif index == "" :
        ^
    IndentationError: expected an indented block
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
ParoX
  • 5,685
  • 23
  • 81
  • 152
  • 1
    Related question about IndentationErrors *not* caused by mixed whitespace: [I'm getting an IndentationError. How do I fix it?](//stackoverflow.com/q/45621722) – Aran-Fey Feb 12 '19 at 14:27
  • Syntax errors can also originate from custom modules that are being imported. – Ender Mar 18 '22 at 18:34
  • I can't fathom why this was reopened. If it's reproducible at all (and I can't reproduce it from the source of revision 1), it can only be the same problem (mixed tabs and spaces) as otherwise it wouldn't appear to be lined up properly. The answers given are in agreement, too. Re-closing, this time with the more helpful "how do I fix it" canonical that's been established in the mean time. – Karl Knechtel Jan 30 '23 at 22:17
  • @Aran-Fey that's the same thing, not "related" - it covers the mixed whitespace problem too, and was explicitly designed to. – Karl Knechtel Jan 30 '23 at 22:18
  • I wanted to post this as an answer but the question has been closed, so I'm posting as a comment. This is the first result in the search engine. The suggested other question contains an answer that indirectly covers this case. In Python 3.10.9 I had an empty method followed by another method in a class definition. This resulted in error **IndentationError: expected an indented block after function definition on line 10**. Adding `pass` to the empty method fixes it. Code to reproduce: `class A: def a(): def b(): print ('Hi!')` – gregn3 Apr 15 '23 at 13:15

6 Answers6

98

As the error message indicates, you have an indentation error. It is probably caused by a mix of tabs and spaces.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • 5
    First thing I thought, so I converted all tabs to 4 spaces. – ParoX Dec 15 '10 at 03:16
  • 1
    @BHare: And...that didn't fix it? I just copied and pasted your code, and added one line, `trashed_files = ['a','b']` so that it would run...didn't give me any indentation errors. – mpen Dec 15 '10 at 03:17
  • I did the replace again, and then I reworte the if len(trashed_files) == 0 : line because it was tabbing weird on stackoverflow...turned out that was the issue...no idea why/what tho. It looked the same in nano. – ParoX Dec 15 '10 at 03:24
  • 1
    @BH: see, tabs are dangerous! :p – Katriel Dec 15 '10 at 03:32
  • @katrielalex Exactly, even i got the same error, this post helped me... thanks all – max Oct 18 '12 at 05:41
  • @mpen, Awesome explanation.+1 – Md Haidar Ali Khan Feb 27 '18 at 12:53
51

There are in fact multiples things you need to know about indentation in Python:

Python really cares about indention.

In a lot of other languages the indention is not necessary but improves readability. In Python indentation replaces the keyword begin / end or { } and is therefore necessary.

This is verified before the execution of the code, therefore even if the code with the indentation error is never reached, it won't work.

There are different indention errors and you reading them helps a lot:

1. "IndentationError: expected an indented block"

They are two main reasons why you could have such an error:

- You have a ":" without an indented block behind.

Here are two examples:

Example 1, no indented block:

Input:

if 3 != 4:
    print("usual")
else:

Output:

  File "<stdin>", line 4

    ^
IndentationError: expected an indented block

The output states that you need to have an indented block on line 4, after the else: statement

Example 2, unindented block:

Input:

if 3 != 4:
print("usual")

Output

  File "<stdin>", line 2
    print("usual")
        ^
IndentationError: expected an indented block

The output states that you need to have an indented block line 2, after the if 3 != 4: statement

- You are using Python2.x and have a mix of tabs and spaces:

Input

def foo():
    if 1:
        print 1

Please note that before if, there is a tab, and before print there is 8 spaces.

Output:

  File "<stdin>", line 3
    print 1
      ^
IndentationError: expected an indented block

It's quite hard to understand what is happening here, it seems that there is an indent block... But as I said, I've used tabs and spaces, and you should never do that.

  • You can get some info here.
  • Remove all tabs and replaces them by four spaces.
  • And configure your editor to do that automatically.

2. "IndentationError: unexpected indent"

It is important to indent blocks, but only blocks that should be indent. So basically this error says:

- You have an indented block without a ":" before it.

Example:

Input:

a = 3
  a += 3

Output:

  File "<stdin>", line 2
    a += 3
    ^
IndentationError: unexpected indent

The output states that he wasn't expecting an indent block line 2, then you should remove it.

3. "TabError: inconsistent use of tabs and spaces in indentation" (python3.x only)

  • You can get some info here.
  • But basically it's, you are using tabs and spaces in your code.
  • You don't want that.
  • Remove all tabs and replaces them by four spaces.
  • And configure your editor to do that automatically.


Eventually, to come back on your problem:

Just look at the line number of the error, and fix it using the previous information.

Xavier C.
  • 1,921
  • 15
  • 22
  • That's the best answer I've found to understand it (#2 nailed it). Thanks! – JorgeAmVF Nov 14 '18 at 07:41
  • Great answer! I have been caught out by a previous else: block containing only commented-out lines causing this case 2 error on the following block. – Jonathan Jul 09 '20 at 07:48
3

I had this same problem and discovered (via this answer to a similar question) that the problem was that I didn't properly indent the docstring properly. Unfortunately IDLE doesn't give useful feedback here, but once I fixed the docstring indentation, the problem went away.

Specifically --- bad code that generates indentation errors:

def my_function(args):
"Here is my docstring"
    ....

Good code that avoids indentation errors:

def my_function(args):
    "Here is my docstring"
    ....

Note: I'm not saying this is the problem, but that it might be, because in my case, it was!

Community
  • 1
  • 1
maxomai
  • 31
  • 2
1

You might want to check you spaces and tabs. A tab is a default of 4 spaces. However, your "if" and "elif" match, so I am not quite sure why. Go into Options in the top bar, and click "Configure IDLE". Check the Indentation Width on the right in Fonts/Tabs, and make sure your indents have that many spaces.

1

In Python, an indented block means there everything must be written in a manner. In my case, I wrote it this way:

 def btnClick(numbers):
 global operator
 operator = operator + str(numbers)
 text_input.set(operator)

Note: it gives me an error until I write it in this way such that "giving spaces" then it's giving me a block as I am trying to show you in the function in the below code:

def btnClick(numbers):
___________________________
|global operator
|operator = operator + str(numbers)
|text_input.set(operator)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • It is nearly incomprehensible. Can you [fix it](https://stackoverflow.com/posts/50288002/edit)? (But *** *** *** *** *** ***[without](https://meta.stackexchange.com/a/131011)*** *** *** *** *** *** "Edit:", "Update:", or similar - the answer should appear as if it was written today).) – Peter Mortensen Jun 08 '23 at 09:30
0

This is just an indentation problem since Python is very strict when it comes to it.

If you are using Sublime Text, you can select all, click on the lower right beside 'Python' and make sure you check 'Indent using spaces'. Choose your Tab Width to be consistent, and then Convert Indentation to Spaces to convert all tabs to spaces.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rad Apdal
  • 442
  • 1
  • 6
  • 16