Python invalid syntax in print

Go To StackoverFlow.com

0

Sorry I am not familar with Python...

It gives me the following error message

  File "gen_compile_files_list.py", line 36
    print 'java files:', n_src
                      ^
SyntaxError: invalid syntax

I.e. caret points to last quote. What's wrong with it?

OS Windows 7, Python version 3.2.2

2012-04-05 21:25
by Suzan Cioc
post the code around line 36. perhaps a missing bracket or something else - j13r 2012-04-05 21:26
instead of comma, use + or it maybe because n_src is not a strin - hjpotter92 2012-04-05 21:27
possible duplicate of Syntax error on print with Python 3bernie 2012-04-05 21:28


4

On Python 3, print is a function. You need this:

print('java files:', n_src)
2012-04-05 21:27
by David Heffernan
Is it possible to run python 3 in 2 compatibility mod eor something - Suzan Cioc 2012-04-05 21:28
No it is not. You can do it the other way around, use Python 3 features in Python 2. If you want Python 2 syntax, use Python 2 - David Heffernan 2012-04-05 21:28
@DavidHeffernan, there is 2to3, which can convert such code to be compatible with Python 3 - Matthew Flaschen 2012-04-05 21:30
It's worth noting you can have Python 3 and Python 2 installed at the same time - Gareth Latty 2012-04-05 21:30
@MatthewFlaschen Yes there is, but that's not the question that I was asked - David Heffernan 2012-04-05 21:30
@DavidHeffernan, your comment wasn't wrong, but I felt 2to3 fell under "or something" - Matthew Flaschen 2012-04-05 21:37
@Matthew pretty much anything falls under "or something" if you want to be like that. 2to3 sounds over the top for a Python novice - David Heffernan 2012-04-05 21:47


2

print changed syntax between Python2 and Python3; it is now a function.

You would need to change:

 print 'java files:', n_src

to

 print('java files:', n_src)

Alternatively, you can try converting the code from Python2 to Python3 syntax with the 2to3 tool. Here is more information on the transition if you are interested. This way you can maintain a single code base that works on both versions.

As you are not familiar with python, try installing Python 2 instead and running the code with that.

2012-04-05 21:28
by j13r


1

print is a function in Python 3+. So:

print ('java files:', n_src)
2012-04-05 21:28
by Andrew Jaffe
Ads