Tuesday, July 24, 2012

Python: weird SyntaxError: invalid syntax

The code was apparently normal:

def myFunctionTestOK():
   return "hi there ok"

def myFunctionTestFail():
   return "hi there fail"

and the error was:

Traceback (most recent call last):  
  File "C:\mytest.py", line 5
    return "hi there fail"
                          ^
SyntaxError: invalid syntax

I was using the Pydev Eclipse Plugin. After a lot of time trying to identify the error I decide to open the file using Notepad ++ http://notepad-plus-plus.org/

clicking in the 'show all characters' button:


I found out that there was missing a (LF) just after the line return "hi there fail":




so I put the cursor just after "hi there fail" and pressed enter:




And finally deleted the line with the single (CR):




Now everything works smoothly.

No comments: