VikkyB
VikkyB

Reputation: 1450

invalid Syntax Error in python print statement using File

print('Group output sizes: |A| = {}, |B| = {}'.format(len(A),len(B)),    file=stderr)
                                                                             ^
SyntaxError: invalid syntax

Can anyone please help what this error is about? I initially thought its because of print syntax but it is not I think.

Please help.

Upvotes: 7

Views: 6842

Answers (2)

user2555451
user2555451

Reputation:

It seems as if you are trying to use Python 3.x's print function in Python 2.x. In order to do so, you need to first import print_function from __future__:

Place the following line at the very top of your source file, after any comments and/or docstrings:

from __future__ import print_function

Below is a demonstration:

>>> # Python 2.x interpreter session
...
>>> print('a', 'b', sep=',')
  File "<stdin>", line 1
    print('a', 'b', sep=',')
                       ^
SyntaxError: invalid syntax
>>>


>>> # Another Python 2.x interpreter session
...
>>> from __future__ import print_function
>>> print('a', 'b', sep=',')
a,b
>>>

Upvotes: 5

NPE
NPE

Reputation: 500227

It looks like you are trying to use the Python 3 print syntax in Python 2.

Either use a Python 3 interpreter, or rewrite the print like so:

print >>sys.stderr, 'Group output sizes: |A| = {}, |B| = {}'.format(len(A),len(B))

Upvotes: 0

Related Questions