Search code examples
xcodepyobjc

How does Xcode pick which Python to run?


Using the Python templates for Xcode, I have a simple project running on one machine and failing on two others, for different reasons. The failure I solved is that even though main.m has:

Py_SetProgramName("/usr/bin/python");

The actual running Python as seen from main.py with this:

import sys
print sys.version

is different. And the problem was that this other Python didn't have objc installed. So the question is: short of cleaning up my machine, how can I direct Xcode to use the system Python?

Edit:

This is a problem that seems like it is trivial, but it isn't. It seems so because when I specify Mac OS X 10.5 as the Base SDK, Xcode links against that Python.framework, as I can see by double-clicking the icon in Linked Frameworks in the Xcode project.

Each Version in Python.framework has a key/value pair in its Info.plist: CFBundleExecutableName/Python. Luckily (for the forensics) this Python in the Version 2.5 framework launches 2.6.1! Yet by printing sys.version (from main.py) or by snooping on the loader (by doing export DYLD_PRINT_LIBRARIES=1), I can see that the PyObjC App actually launches Python 2.5.4.

The line above with Py_SetProgramName.. is irrelevant---it can be commented out and everything works fine.

So what it looks like is that Python.framework controls which Python is launched, it doesn't do this in the way I expected, and it might be controlled through an initialization routine that is opaque, since it's controlled by Apple. It seems the best way to "control" the situation is to change the Base SDK.


Solution

  • Sorry to answer my own question but I was just confused. This part is an error

    Python in the Version 2.5 framework launches 2.6.1

    caused by my not realizing that what's going on here (from the Version/2.6 directory)

    > ./Python
    -bash: ./Python: cannot execute binary file
    > Python
    Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
    

    is that we're searching my $PATH and launching /opt/local/bin/python. Apologies for the confusion.

    To summarize, setting the SDK should get the desired Python to run as we can see by snooping on the loader:

    > export DYLD_PRINT_LIBRARIES=1
    > ~/Desktop/X/build/Debug/X.app/Contents/MacOS/X
    dyld: loaded: /Users/telliott_admin/Desktop/X/build/Debug/X.app/Contents/MacOS/X
    dyld: loaded: /System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa
    dyld: loaded: /System/Library/Frameworks/Python.framework/Versions/2.5/Python
    ..