python version conflict with json.dumps

epsilones picture epsilones · Dec 3, 2015 · Viewed 10.9k times · Source

I am a newb to python. I am running a script I got on the web :

python file.py

I get this :

File "file.py", line 293
    print json.dumps(evaluate(), indent=4)
             ^
SyntaxError: invalid syntax

I read it is related to python version, that should be some 2.7. So I downloaded pyenv. And I set the right version in the directory that contains file.py : pyenv local 2.7.10. But I still get the same error.

(For information, I am trying to install blockchain tool : ethereum)

Answer

Brian Cain picture Brian Cain · Dec 3, 2015

Python 3.x changed print statement to be print functions

Python 2.x:

print "Hello World" 

Python 3.x

print("Hello World")

So because you are running on python 3.x you will need to update your code to use the 3.x print style (e.g., print function calls).

print( json.dumps(evaluate(), indent=4) )