views:

99

answers:

3

I have a MacBook Pro with Snow Leopard, and the Python 2.6 distribution that comes standard. Numpy does not work properly on it. Loadtxt gives errors of the filename being too long, and getfromtxt does not work at all (no object in module error). So then I tried downloading the py26-numpy port on MacPorts. Of course when I use python, it defaults the mac distribution. How can I switch it to use the latest and greatest from MacPorts. This seems so much simpler than building all the tools I need from source...

Thanks!

+1  A: 

You need to update your PATH so that the stuff from MacPorts is in front of the standard system directories, e.g., export PATH=/opt/local/bin:/opt/local/sbin:/opt/local/Library/Frameworks/Python.framework/Versions/Current/bin/:$PATH.

UPDATE: Pay special attention to the fact that /opt/local/Library/Frameworks/Python.framework/Versions/Current/bin is in front of your old PATH value.

Hank Gay
+4  A: 

First of all, add the MacPorts path (/opt/local/bin) to your $PATH. In .bashrc (or whatever shell config file you use):

export PATH="/opt/local/bin:${PATH}"

If you have multiple versions of Python installed via MacPorts, and/or want to easily switch between the MacPorts and Apple distributions, you can install the python_select port as well.

Also note that the MacPorts version of Python 2.6 is installed into /opt/local/bin/python2.6, so to use that interpreter, you'll have to do one of three things:

  1. Start the interpreter using python2.6 (not just python).
  2. Set up a shell alias so that python calls python2.6 (alias python=python2.6).
  3. Manually set up a symlink from /opt/local/bin/python -> /opt/local/bin/python2.6.
  4. Use python_select to set the Python used by calling python.

Options #3 or #4 are probably the best bet.

mipadi
The MacPorts path is in my $PATH. When I call python in the shell, it opens the interpreter that was installed with my mac, so I assume that is the distribution it uses when I run a script...?? Thanks for the help!
lollygagger
Is `/opt/local/bin` in the *front* of `$PATH` (or, at least, before `/usr/bin`)?
mipadi
It's there twice, is that the problem?? `echo $PATH` `/opt/local/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin:/usr/X11/bin`
lollygagger
Ah -- I think it's because MacPorts' Python gets installed as `/opt/local/bin/python2.6`. I updated my answer to reflect this revelation.
mipadi
+10 to option 4 - it's incredibly easy to use. `python_select -l` lists available options - you'll want `python_select python26`
nearlymonolith
A: 

The existing answers are quite useful, but I noticed that neither of them spell out how to make the change stick. If you're not familiar with the unix command line this might be important.

First, and explanation: In unix based operating systems, important configuration information in the shell is stored in things called environment variables. The environment variable called PATH directs your shell to a list of places to look for programs. When you type a command, it starts at the leftmost end of the PATH variable, and looks in that folder for the program you tried to run. If it finds it, it runs it; else it looks in the next folder. When you have multiple versions of the same program installed, you can use the PATH variable to give one precedence.

To make use of this, put the folder with the shiny new version in front of the path, like this:

PATH=/opt/local/bin:/usr/bin:/usr/local/bin

To make this change in a single version of your shell, you can type

export PATH=/opt/local/bin:/usr/bin:/usr/local/bin

To make the change in every shell you open, you need to instruct your shell to set this variable every time it starts. There is a file called .bashrc, and another one called .bash_profile that bash will read when it starts up. The .bashrc file is generally used to contain instructions for all shells, and .bash_profile is used to contain instructions only for interactive shells. So, to make this change stick, you can edit /Users/yourname/.bashrc to include a line like this:

export PATH="/opt/local/bin:$PATH"

What that does is add /opt/local/bin to the front of the path variable while leaving the rest of the path alone. If that change doesn't seem to work, you will need to either ensure .bashrc is getting called by adding source $HOME/.bashrc to your .bash_profile script, or just move the necessary line into .bash_profile.

Benson