Forums

Syntax error because of indentation

Hi all,

I have having problems with syntax error in a form class. Code looked good on the editor, so I tested it and server did rise the exception. Problem seemed related with indentation. Notification of the editor appeared after a copy & paste. Not from an external source, but between files opened on pythonanywhere editor.

Rewriting the whole code solves the problem. However, I like copy paste so I finally found some workaround to use it and fix this syntax error:

I will use a form class as an example. Given:

class ExampleForm(forms.ModelForm):
    class Meta:
        model = Example
    name = forms.CharField(
        label='Name',
                ...
    )
    other = forms.CharField(
        label='Other',
                ...
    )

And you have a Syntax Error on 'other' line. Then, do:

  1. From the previous variable end character ')', push enter.
  2. Cursor will be put automatically indented. Return to the beginning of the line with backspace.
  3. Now, use Tab to put the cursor indented again.
  4. Write the first character of the next line ('o' in this example).5. Use Delete until next line go up and you replace the first character of it.

It worked for me, I hope it does it for other people too.

Kind regards

IƱaki

My guess is this came from mixing tabs and spaces... I'll make a note that we should add a "show whitespace markers" feature to the editor.

In the meantime, one alternative way of dealing with it would be:

  • Open the "find and replace" menu in the editor (Ctrl+H)
  • copy a tab character from another editor into the find box
  • enter 4 spaces as the replace

And replace away!

Or, in a Bash console, you can use sed:

sed -i "s/\t/    /g" file_to_fix.py

You just saved me!!! Thanks!!!

You saved me too.

:-D

Harry, thanks

Hello, I really love the P.A services. As a suggestion, kindly implement an indentation option similar to sublime editor. It will be much easier to edit code. Thanks a lot for the great services.

Which indentation option do you mean? (I use Sublime myself when editing stuff locally, so I'll be able to see exactly what you mean if you give some more details.)