Switch python distributions
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, 开发者_如何学Goand 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!
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:
- Start the interpreter using
python2.6
(not justpython
). - Set up a shell alias so that
python
callspython2.6
(alias python=python2.6
). - Manually set up a symlink from
/opt/local/bin/python
->/opt/local/bin/python2.6
. - Use
python_select
to set the Python used by callingpython
.
Options #3 or #4 are probably the best bet.
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.
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
.
精彩评论