How do I properly update Python on Mac OS X
Solution 1
I finally found how to do it. The installer made a python 2.6 folder in /Application In this folder is a script called Update Shell Profile.command I just needed to execute it and now it's working.
Solution 2
Two questions:
- Where did the new Python get installed?
- What does your $PATH look like?
A default set-up on Unix-like systems is that user-installed software gets installed in /usr/local/bin, and most *nix distros put that directory before system-wide directories in the default $PATH variable (which is how your shell knows where to look for programs). That way, if you install something new, the new item gets found first.
However, a default OS X $PATH looks like this:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
The problem is that by putting /usr/local/bin so late, the system-wide Python (which is at /usr/bin and so earlier in your $PATH) keeps getting hit.
My answer is to make my $PATH look like this on a Mac:
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin
To do that, create a file .profile in your home directory and add something like this:
#### Let's take care of our $PATH
# A backup of the original $PATH
# /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
# My preferred order - /usr/local goes first, damn it!
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin
Edit: I'm following up here, rather than in comments because it's getting too long for a comment. The line in your .profile with /sw/bin/init.sh
comes from the package manager Fink, which I'm assuming you use (or used at some point). The other line seems to suggest that at some point you installed MacPython and it rewrote your $PATH
for you. I don't know MacPython, but if it's this site, then it hasn't been updated since 2004. It also doesn't seem to talk about any version of OS X beyond 10.3, which is not very current.
So now I'm more confused: when did you update Python? How did you update it? What version of OS X are you running?
Related videos on Youtube
Philippe Mongeau
Updated on September 17, 2022Comments
-
Philippe Mongeau over 1 year
If I download python from the Python website and try to install it with the installer, it installs, but I need to specify that I want the new version of Python when I run my programs from terminal. If I just type:
python app.py
It runs with the old version.
How do can I set the new version as default?
-
Telemachus over 14 yearsHe should absolutely NOT undo the system's default Python in this way. OS X expects it to be where it is. This would be a huge mistake.
-
Bill Weiss over 14 yearsIt's a legitimate answer to the question. I've done it before without anything blowing up. Any other method won't result in programs with a bang path of /usr/bin/python using the new right one.
-
Telemachus over 14 years@Bill: I think it's a terrible approach. You can always script with a shebang of
#!/usr/bin/env python
, and you should not replace a system-wide perl or python if you can avoid it. -
Philippe Mongeau over 14 yearsest -r /sw/bin/init.sh && . /sw/bin/init.sh # Setting PATH for MacPython 2.6 # The orginal version is saved in .profile.pysave PATH="/Library/Frameworks/Python.framework/Versions/2.6/bin:${PATH}" export PATH This is how my .profile looks, I'm not sure what I should do since it's different from your's. [Is' there a way to format code in comments?]
-
Telemachus over 14 years@Philippe: You can format code by using backquotes, just as in posts. See the post itself for my response to the larger issue.
-
Doug Harris over 14 yearsI'm with Telemachus on this. Let the standard Mac stuff remain as is. Custom installations go in /usr/local or /opt/local and adjust PATH and other environment variables as necessary.
-
Philippe Mongeau over 14 yearsI finally found how to solve it. The installer made a folder in my application directory wich contains the idle, some examples and a script to update the command line. And for the MacPython thing, I think it got installed with xcode. Thanks for trying to help me anyway.
-
las3rjock over 14 years@Telemachus I believe MacPython refers to the .dmg installer from python.org/download
-
jweede over 14 yearsYep. Python 2.6 now includes a script to make it the default on the command line.
-
Telemachus over 14 years@Las3rjock: Cool, that's much better than my guess! Thanks for the link.