How can I update the default Python installation/fix my $PATH on Mac OS X 10.6.7?

I know this question has been asked before on this site, and I have found a couple worthy discussions, but the solutions I've found and tried do not work. I want to learn Python and I'm having trouble getting it set up properly on my Mac. I'm running OS X 10.6.7.

I've downloaded the Python 3.2 installer from python.org and installed it, which installed to /Library/Frameworks/Python.framework/Versions/3.2/bin. This is fine for now as I can simply use IDLE to develop. In order to use 3.2 from the command line using python I tried to reorder my $PATH variable. This is what ~/.profile looks like:

PATH=/Library/Frameworks/Python.framework/Versions/3.2/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin

However, python still launches 2.6.1:

steven-macbook:~ steven$ python --version
Python 2.6.1
steven-macbook:~ steven$ . ./.profile
steven-macbook:~ steven$ python --version
Python 2.6.1
steven-macbook:~ steven$ 

After struggling with that for a while, I decided I should simply uninstall 3.2 and try and update the system installation of Python. I'm unsure of how to properly do that, so I either need to get 3.2 working with the command line or update 2.6.1.


Solution 1:

I just solved this problem by installing Python 3.2.2 on my Mac OS X Lion so I thought I would post how I did it in the hopes someone may find it helpful.

Firstly, I agree with shadesandcolour in that you should not modify the default copy of python that came with your mac because there could be scripts that may not work properly when run using the Python 3.0+ interpreter. Having said that, to install that latest Python (3.2.2 at the time of this post), you should first download the Python installer from the official website. Install as per normal. Run the "Update Shell Profile.command" file in /Applications/Python(ver) folder.

After python has been installed, you need to edit your path by typing "sudo nano /etc/paths" at the command line. Next, enter your password when prompted. Next just "cut" the line "/usr/local/bin" and "paste" it BEFORE the occurrence of "/usr/bin". Save the file, close any open terminals and run a new instance of terminal. Now when you type "python" at the command line, the shell will search through /usr/local/bin first and run the latest copy of python.

Solution 2:

From my experience, using symlinks along with a properly ordered PATH is the way to have multiple versions of Python coexist in OS X or any other nix I can think of. OS X is just a little bit goofy, but it works the same way.

First get the full picture by running the following commands:

which python

Then:

ls -la /usr/bin/python*

and compare with:

ls -la /usr/local/bin/python*

See where the symlinks are pointing.

Based on the PATH you posted, since /usr/bin comes after /usr/local/bin and the others you are probably using the System python interpreter:

/usr/bin/python

If you installed python3.0 it will likely run as:

/usr/local/bin/python

Now that you know what is pointing where, and what interpreter is being loaded from the PATH, you can make the appropriate changes to the PATH and/or symlinks. You can then use the same techniques to confirm the proper changes.

For more good information about python installation on macOS see dive into python3 and farmdev.