Python Language Syntax Error on good code


Example

The gross majority of the time a SyntaxError which points to an uninteresting line means there is an issue on the line before it (in this example, it's a missing parenthesis):

def my_print():
    x = (1 + 1
    print(x)

Returns

  File "<input>", line 3
    print(x)
        ^
SyntaxError: invalid syntax

The most common reason for this issue is mismatched parentheses/brackets, as the example shows.

There is one major caveat for print statements in Python 3:

Python 3.x3.0
 >>> print "hello world"
  File "<stdin>", line 1
    print "hello world"
                      ^
SyntaxError: invalid syntax

Because the print statement was replaced with the print() function, so you want:

print("hello world")  # Note this is valid for both Py2 & Py3