7
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.

2
  • 6
    Don't run Python 3 code in Python 2. Commented Mar 10, 2014 at 20:40
  • 1
    print does not accept keyword arguments in Python 2.x Commented Mar 10, 2014 at 20:40

2 Answers 2

5

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
>>>
Sign up to request clarification or add additional context in comments.

Comments

0

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))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.