Muricula
Muricula

Reputation: 1234

Python version is 3.2 at the prompt, 2.6 in scripts

The default version of python on Snow Leopard is 2.6. I decided to upgrade to 3.2 using an installer from the python website, which installed both 32 and 64 bit versions of python (useful for some libraries.)

The issue is that my python scripts written like 3.2 interpret as 2.6, while my python shell interprets as 3.2 What I wrote may not make sense.

So I have a script which says this:

#!/usr/bin/python
import sys
print(sys.version)#note the python 3 syntax

When I run it I get this:

$ ./test.py 
2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)]

When I run the same thing at the python prompt I get this:

$python
Python 3.2.3 (v3.2.3:3d0686d90f55, Apr 10 2012, 11:25:50) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.version)
3.2.3 (v3.2.3:3d0686d90f55, Apr 10 2012, 11:25:50) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
>>> 

The question is, how do I fix this? I assume it is an installation issue. Edited to make the name more useful.

Upvotes: 0

Views: 118

Answers (4)

HerrB
HerrB

Reputation: 176

According to what you wrote in your clarification, at the end of your .bash_profile there is an alias being defined:

alias python=python3.2

This is the culprit. Consider dropping it and you have a more transparent and intuitive situation.

"which python" (I assume it is /usr/bin/which) didn't solve your perceived mystery because it is not aware of aliases. It only finds a python binary in your path.

Upvotes: 1

Muricula
Muricula

Reputation: 1234

I still don't know why running python in a script gave me the 2.6 python, even though running python at the prompt gave me the 3.2 python. I did fix it. I found the python3 binary and renamed the problematic python binary in my /usr/bin. Then I made a symlink from /usr/bin/python to /Library/Frameworks/Python.framework/Versions/3.2/bin/python3

I'm still curious what the original problem was, but it's fixed now.

Upvotes: 0

Andy D
Andy D

Reputation: 916

It's important to understand what has precedence in your path (/usr/bin or /usr/local/bin), what does 'which python' reveal in the terminal? You'll want to edit your .bash_profile and add in the path to the new python binary, and change the #!/usr/bin/python in your scripts to point to the new python, something like #!/usr/local/bin/python - wherever the installer put Python 3.x

Upvotes: 0

Andrew Gorcester
Andrew Gorcester

Reputation: 19983

At the command line, type which python and report the results. I bet your path is finding Python 3.3 for some reason and it's not the same one as in /usr/bin/python. If they are different, use #!/usr/bin/env python in your script to use your path env variable instead of an absolute path to find python.

Upvotes: 1

Related Questions