2

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.

1
  • For additional information, I just found this question: stackoverflow.com/questions/1499572/… I have the same problem as this question. Hope this will help for your answers. Commented Nov 20, 2009 at 7:42

3 Answers 3

7

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
Sign up to request clarification or add additional context in comments.

5 Comments

I heard that 'export' only lasts for the terminal session, is it true?
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.
For posterity: python_select command doesn't seem to exist for me. To select the python you want macports to use one must do sudo port select python --list and then sudo port select python python27 (for 2.7 for example). Also, as Ned said, make sure that /opt/local/bin comes before /usr/bin in your $PATH. This last part ensures that python command uses the MacPorts version of python and not Apple's.
@Nick: it appears that was a fairly recent change in MacPorts. trac.macports.org/changeset/78591
Thanks for the detail. I did a 'port cat python_select' and saw "This port installs files that allow 'port select' to...". That's how it all started. =)
0

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.

1 Comment

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.
0

'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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.