1

I am trying to print a list of groups in AWS.

Of these lines only one line is producing an indentation error. If I use this code:

group_list = client.list_groups()
print("Group List for %s:")
for group in group_list['Groups']:
    group_name = group['GroupName']
    print(group_name)

I get this error:

  File ".\aws_iam_utils.py", line 966
    print(group_name)
                    ^
TabError: inconsistent use of tabs and spaces in indentation

But if I remove the line print(group_name) the program runs.

I tried both adding that line with 4 spaces and when that didn't work I added it with one tab.

It's at the exact same indentation level as the previous line. So I don't know why this error is happening.

bluethundr
  • 1,005
  • 17
  • 68
  • 141
  • Check that the entire file is consistent. – Blorgbeard Apr 15 '19 at 18:36
  • What if you delete both indented lines, and add them again with 4 spaces? If `group_name = group['GroupName']` is currently indented with a tab, and if your text editor inserts four spaces when you press "tab", then rewriting `print(group_name)` won't do anything. – Kevin Apr 15 '19 at 18:37
  • Possible duplicate of [inconsistent use of tabs and spaces in indentation](https://stackoverflow.com/questions/5685406/inconsistent-use-of-tabs-and-spaces-in-indentation) – Mercury Platinum Apr 15 '19 at 18:37

2 Answers2

1

Python is heavily reliant on consistent code indentation to identify code blocks, and the TabError exception occurs when you use a different mix of tabs and/or spaces to indent lines in the same code block.

You should check the tabs/spaces used to indent the line before the line of error:

    group_name = group['GroupName']

and make sure it has the same mix of tabs and spaces that are used to indent the line of error:

    print(group_name)

Moreover, it is generally discouraged to use tabs at all for indentation in Python precisely because it is easy to run into such a problem. I would recommend that you convert all your tabs to 4 spaces to be more easily able to spot such inconsistent indentation issues.

blhsing
  • 91,368
  • 6
  • 71
  • 106
1

When you mix spaces and tabs in your indentation, bad things happen, this error being a common problem. This means you either used spaces for the first statement in the loop and a tab for the second one, or you used a tab for the first statement and spaces for the second statement.

Remove all indentation and then add it again. Keep it either all spaces or all tabs and it will work fine. The common convention is to use spaces unless you're working with code that's using tabs, so unless you need to use tabs for compatibility reasons, use spaces instead. See this section of the style guide.

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42