python version conflict with json.dumps
Asked Answered
S

1

5

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)

Sweltering answered 3/12, 2015 at 0:1 Comment(6)
Which operating sytem are you using? Does the first line of the python script have something like #!/usr/bin/python in it (the "shebang")? I don't use pyenv so can't say much about it. How about writing an experimental script that uses that same "shebang" (if present) and then just does import sys;print(sys.version) to make something easier to experiment with.Forgiven
@Forgiven yes it is a python script with #!/usr/bin/python and indeed I found this : 3.5.0 |Anaconda 2.4.0. I don't know how to get rid of that - do you have any idea ?Sweltering
Weird environment problems are hard to figure out! /usr/bin/python is usually python 2, which is what you want for the old style print statement (print something as opposed to print(something)). Python 3 usually shows up as /usr/bin/python3. What does which python say? I'm not familiar with anacoda either and it may have something else in the path.Forgiven
@Forgiven which python outputs /myhome/anaconda/bin/pythonSweltering
@Forgiven yes I was able to lake it work by changing the PATH that was set to some path related to anacondaSweltering
Running the script as ./file.py may work also (it may need a chmod u+x file.py).Forgiven
U
17

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) )
Unhitch answered 3/12, 2015 at 0:25 Comment(1)
There are other differences between 2 and 3 besides just print. Converting may be a major lift.Forgiven

© 2022 - 2024 — McMap. All rights reserved.