How to debug indentation errors in python

Go To StackoverFlow.com

11

I am trying to write my very first python script. This was working but then after some slight refactoring I have, apparently, broken the indentation. I can not determine what is the problem. The interpretor complains about the following method. Can someone point it out?

def dataReceived(self, data):
    a = data.split(':')
    print a
    if len(a) > 1:
        command = a[0]
        content = a[1]

        msg = ""
        if command == "iam":
            self.name = content
            msg = self.name + " has joined"

        elif command == "msg":
            msg = self.name + ": " + content
            print msg

The error reads: File "python_server.py", line 17 a = data.split(':') ^ IndentationError: expected an indented block

2012-04-05 02:45
by Nick


3

You start using a text editor that allows you to show indents, and you become consistent about using spaces instead of tabs, and you enforce that in your editor.

2012-04-05 02:48
by dkamins
Yeah.. you're right. I opened the file in another editor and the indentation was different. Sublime Text was tricking me. Thanks - Nick 2012-04-05 02:53
5 years later and Sublime Text still hasn't fixed this issue - Joe 2017-12-27 07:27
I use this settings in Sublime. Helps a lot. "translate_tabs_to_spaces": true, "ensure_newline_at_eof_on_save": true, "trim_trailing_white_space_on_save": truerosinghal 2018-04-21 11:15


46

I encountered a similar problem using Sublime Text 2.

To solve, click on the "Tab Size" at the bottom of the editor, and choose "Convert Indentation to Tabs".

2012-07-22 17:08
by samwize


3

There are a great number of things you can do here:

  1. Use an editor that can show control characters (like vi with set list).
  2. Use a hex dumper program like od -xcb.
  3. Just delete the white space at the start of that line and re-insert it (may want to check the preceding line as well).
2012-04-05 02:49
by paxdiablo


0

Try Editra - www.editra.org

Your code looks fine, syntax seems fine...your text editor may be creating your errors. Review your file with Editra to see/review indentation levels.

Editra saved my sanity - I thought I had correct syntax when viewing my script in Text Editors including Notepad++ with python indent plugin. However, when I would run the script, it would throw off indentation errors every time. I finally opened the script up in Editra, and I could see the problem. Notepad++ and other text editors did not show correct indentations/tabs/spaces. Editra showed errors e.g. unexpected spaces, tabs - which I was able to correct.

Editra will auto-indent your script [and show errors -tabs, spaces -that may not show up in other text editors].

If you have indent errors it will show up as blue underlined segment;

If you are writing script [adding/deleting lines] Editra will auto-indent the script.

**I would suggest opening your script and editing it in Editra.

Hope this helps! Best of luck. str8arrow

2015-04-05 14:02
by str8arrow


0

if you're using the "Sublime Text 2" editor, then I found this answer helpful - it details how to turn on whitespace characters and also convert tabs to whitespaces

sublime-text-2-view-whitespace-characters

2018-02-13 17:37
by robbie70
Ads