tags:

views:

514

answers:

3

I installed a couple of pythons in different versions with macports, and the apple python 2.6 is also working. Now I need to run a program which requires MySQLdb package support in python, and this package was installed to the python I installed by macports. The program tells me that there is no MySQLdb installed, so I guess it is the apple python working for that program.

I searched for some help and found python_select for switching between pythons. However after the command

$>sudo python_select python25

told me that it selected the version "python25" for python, when I type

$>python

it is still apple python 2.6 that launches.

The question is that how can I make python25(the one with MySQLdb) work for the program rather than apple python?

Another important thing, the program is NOT a .py file and needs to be compiled before running. So do I need to re-install this program? My Mac OS version is Snow Leopard 10.6.

Any answer is appreciated.

A: 

First, I am not sure with Mac, coz I never use it before.

but in Linux, when I do whereis python

It will show like /usr/bin/python /usr/local/bin/python ....etc

in my .bashrc file, I just export PATH=/usr/local/bin:/usr/bin:$PATH when I want /usr/local/bin more priority

or you still can run like

/usr/bin/python yourpython.py

or

/usr/local/bin/python yourpython.py

depends on your python install locations

just my 2 cents. sorry if my answer dont make you any helps.

S.Mark
Never mind. In mac when I do 'which python' it returns what path 'python' is referring to. If you can tell me how to modify the reference I'll be very appreciated.
Jim
+6  A: 

By default, MacPorts installs user programs (or links to them) in /opt/local/bin. The MacPorts select_python command selects which python instance is linked to /opt/local/bin/python. It has no effect (nor should it) on what Apple installs in /usr/bin, which is where the Apple-supplied python and python2.x commands are.

To invoke the MacPorts python2.5, you either need to ensure that /opt/local/bin precedes /usr/bin on your shell $PATH (you can do this by modifying your .bash_profile or other shell initialization script) or you can simply invoke the desired python with an absolute path reference:

$ /usr/bin/python your-program.py

to use the Apple-supplied default python;

$ /opt/local/bin/python your-program.py

to use the version selected with python_select, or:

$ /opt/local/bin/python2.5 your-program.py

to explicitly select the MacPorts 2.5 one.

EDIT:

To modify your search PATH to use MacPorts, add this line to .bash_profile:

export PATH=/opt/local/bin:/opt/local/sbin:$PATH
Ned Deily
That's pretty helpful. Can you tell me how to modify the '~/.bash_profile'? Many thanks!
Jim
See edit above.
Ned Deily
I heard that 'export' only lasts for the terminal session, is it true?
Jim
Generally, each terminal session starts a new "login" shell which means the commands in your ~/.bash_profile will be executed for each terminal session. The export command tells the shell to pass along the value of that environment variable ($PATH) to any subsequent subprocesses that it creates. So the net effect is that exported variables will be available in all terminal sessions and the programs running in them - unless you explicitly unset them or change their value.
Ned Deily
I see. Many thanks for your answer.
Jim
A: 

'python' in the Mac is just a link. Do a 'which python', 'cd' to the directory in which 'python' resides, and then do an 'ls -a py*'. You shall see where python is pointing too. If you want that python to point to your different version of python, just make it link to the right version.

Sankha