Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The wrong python interpreter is called

I updated my python interpreter, but I think the old one is still called. When I check for the version I get:

$ python -V
Python 3.0.1

But I believe the old interpreter is still being called. When I run the command:

python myProg.py

The script runs properly. But when I invoke it with the command

./myProg.py

I get the error message:

AttributeError: 'str' object has no attribute 'format'

Which apparently is due to the old interpreter being called. How can I fix this? I run Mac OS X 10.5. Has it something to do with the first line:

#!/usr/bin/python

I just started out with python and am not very familiar with interpreted languages, so I am not too sure what is going on.

like image 767
Lucas Avatar asked May 24 '09 16:05

Lucas


4 Answers

According to the first line of the script, #!/usr/bin/python, you are calling the Python interpreter at /usr/bin/python (which is most likely the one that ships with Mac OS X). You have to change that path to the path where you installed your Python 3 interpreter (likely /usr/local/bin/python or /opt/local/bin/python); or you can just change that line to read #!/usr/bin/env python, which will call the python listed first in your PATH variable (which seems to be the newer version you installed).

like image 154
mipadi Avatar answered Oct 21 '22 07:10

mipadi


Firstly, the recommended shebang line is:

#!/usr/bin/env python

This will make sure the python interpreter that is invoked when you ./foo.py is the same interpreter that is invoked when you invoke python from the command line.

From your description, I suspect that if you did:

which python

It would not give you /usr/bin/python. It would give you something else, which is where the python 3 interpreter lives. You can either modify your shebang line to the above, or replace the path to the python interpreter with the path returned by which.

like image 30
freespace Avatar answered Oct 21 '22 07:10

freespace


Try which python. I will tell you which python interpreter is used in your environment. If it is not /usr/bin/python like in the script, then your suspicion is confirmed.

like image 3
bbuser Avatar answered Oct 21 '22 06:10

bbuser


It's very possibly what you suspect, that the shebang line is calling the older version. Two things you might want to check:

1) what version is the interpreter at /usr/bin/python:

/usr/bin/python -V

2) where is the python 3 interpreter you installed:

which python

If you get the correct one from the command line, then replace your shebang line with this:

#!/usr/bin/env python

Addendum: You could also replace the older version of python with a symlink to python 3, but beware that any major OS X updates (ie: 10.5.6 to 10.5.7) will likely break this:

sudo mv /usr/bin/python /usr/bin/python25
sudo ln -s /path/to/python/3/python /usr/bin/python
like image 3
EvanK Avatar answered Oct 21 '22 06:10

EvanK