Xcode 3 Cocoa-Python Application

Discussion in 'Mac Programming' started by Daspeed, Oct 29, 2007.

  1. Daspeed macrumors newbie

    Aug 17, 2006
    Last friday I performed an update from Tiger to Leopard and also installed Xcode 3. As I'd like to experiment with python/objc, I started a 'Cocoa-Python Application' from the project templates. But whatever I do, I keep getting the following error:

    So it seems that it can't find (py)objc. That's strange because I thought that it would come with Leopard. I don't know what to do anymore, because a custom installation doesn't seem to work either. Does anyone have a solution for this problem?
  2. kainjow Moderator emeritus


    Jun 15, 2000
    This still happens when you make build a new project without any changes?

    It works fine for me. Maybe you need to do a clean install of Leopard.
  3. Daspeed thread starter macrumors newbie

    Aug 17, 2006
    Unfortunately it also happens when I start a new project. I even reinstalled Xcode a couple of times and did a disk permission restore, none of which seem to help.

    Maybe a clean install of Leopard will help, but that would cost me a lot of time, and I'm really hoping for a better solution.
  4. Daspeed thread starter macrumors newbie

    Aug 17, 2006
    After removing Python from the Frameworks folder and re-installing Xcode, it seems to work. For now at least...
  5. LaskoVortex macrumors newbie

    Jan 15, 2008
    Thanks Daspeed. The one to remove is in /Library/Frameworks, which was installed by MacPython Installers. I found I only needed to remove the Python.framework there (actually just temporarily moved it to my Desktop, since I use it considerably). I did not need to re-install xcode.

    This situation kind of sucks, because I think it means your python-cocoa app won't work for people who have done these installs. Probably the MacPython people need to be contacted about this.
  6. mcow macrumors newbie

    Feb 16, 2008
    A fix

    I have the same issue when creating a new PyObjC project. Moving Python.framework outside of my /Library/Frameworks folder "fixes" it, but as LaskoVortex pointed out, this is no good if you actually intend to distribute your app.

    I have found a simple fix, though. As far as I can tell, the version of Python 2.5 installed by MacPython has no problem running the PyObjC module. The problem is that the PyObjC modules are in the other version of the framework (the one in /System/Library/Frameworks), and the MacPython version isn't aware of them. So we need to add the path to these modules to the "PYTHONPATH" environment variable.

    So here's the fix:

    In your XCode project, open the "main.m" file (not "main.py"). Near the beginning of the file, you should see:

    int main(int argc, char *argv[])
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *resourcePath = [mainBundle resourcePath];
    NSArray *pythonPathArray = [NSArray arrayWithObjects: resourcePath, [resourcePath stringByAppendingPathComponent:@"PyObjC"], nil];
    setenv("PYTHONPATH", [[pythonPathArray componentsJoinedByString:@":"] UTF8String], 1);
    Replace this line:
    NSArray *pythonPathArray = [NSArray arrayWithObjects: resourcePath, [resourcePath stringByAppendingPathComponent:@"PyObjC"], nil];
    with this:
        NSArray *pythonPathArray = [NSArray arrayWithObjects: resourcePath, [resourcePath stringByAppendingPathComponent:@"PyObjC"], @"/System/Library/Frameworks/Python.framework/Versions/Current/Extras/lib/python/", nil];
    And that's it. Build and run the project.

    Note: I have not tested this very thoroughly but it works with the basic tests I've done. If you use it in your own code, make sure to test everything thoroughly to make sure it works.

    This fix should make your project work regardless of whether MacPython is installed. If MacPython is installed, your app will run using the version of Python installed by MacPython. So if that version is older than 2.5, it may not be able to load the objc module (I haven't tested this). Even so, this is way better than not working if any version of MacPython is installed.

    This workaround notwithstanding, Apple really needs to fix this behavior. At the very least, they could do what I've described here. But ideally, they should make PyObjC apps only use the "System" version of the Python framework.
  7. hhas macrumors regular

    Oct 15, 2007
    Python extensions aren't binary compatible over major Python versions, so expect breakage in this situation.

    If you must use the Xcode Python project templates, the best solution would be to avoid linking Python.framework directly, and load up the needed symbols from the system Python at runtime. e.g. See the bootstrap code for py2app's application template for an example of how to do this. (I use the same approach in my PyOSA component if you want a simpler example.)

    Alternatively, you could just use py2app to build your application. I've built several Python-based apps this way and IMO it's the way to go. See the application projects in /Developer/Examples/Python/PyObjC for examples.
  8. GirthP macrumors regular

    Oct 1, 2007

    Will I have to do this with each project??

    I'm starting fresh with XCode and would like to use Python. I must have installed MacPython at some point in my Python self-teaching marathon... months on, months off ;). Could I just uninstall it, since os x 10.5 comes with 2.5?

    Thank you for any input!
  9. mcow macrumors newbie

    Feb 16, 2008
    You can either do it for each project or edit the Python project templates in "/Developer/Library/Xcode/Project Templates/Application/".

    If you're just writing stuff for your own personal use, it's probably easier to just uninstall MacPython. But your app won't run on any system with MacPython installed. If you're writing software that you want to publish, I would recommend either my workaround or one of the ones hhas suggested (which is probably a little more work, but will work in more situations).

Share This Page