6

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?

3 Answers 3

7

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.

1
  • Yep. Python 2.6 now includes a script to make it the default on the command line.
    – jweede
    Aug 26, 2009 at 19:06
5

Two questions:

  1. Where did the new Python get installed?
  2. 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?

5
  • est -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?] Aug 25, 2009 at 21:14
  • @Philippe: You can format code by using backquotes, just as in posts. See the post itself for my response to the larger issue.
    – Telemachus
    Aug 25, 2009 at 23:47
  • I 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. Aug 26, 2009 at 18:06
  • @Telemachus I believe MacPython refers to the .dmg installer from python.org/download
    – las3rjock
    Aug 26, 2009 at 18:53
  • @Las3rjock: Cool, that's much better than my guess! Thanks for the link.
    – Telemachus
    Aug 26, 2009 at 21:20
-1

Find out where python lives:

$ which python
/usr/bin/python

See if it's a symlink:

$ ls -Fal /usr/bin/python
lrwxr-xr-x  1 root wheel  72 May 19 12:02 /usr/bin/python@ -> ../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/python

If so, link /usr/bin/python to the new python:

$ sudo ln -s (new python, maybe /System/Library/Frameworks/Python.framework/Versions/2.x/bin/python) /usr/bin/python

Check if it works:

$ python --version
Python 2.xxx
4
  • 4
    He 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.
    – Telemachus
    Aug 25, 2009 at 20:35
  • It'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.
    – Bill Weiss
    Aug 25, 2009 at 21:02
  • 3
    @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.
    – Telemachus
    Aug 25, 2009 at 21:05
  • 4
    I'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. Aug 26, 2009 at 17:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .