5

I am building a PyObjC app using Xcode 4.6.2 and am trying to figure out how to bundle the Python framework in with my app, so that I can reference it without relying on the host system having a particular Python version.

(I previously asked this question, but since then, I've decided that embedding the framework is the better way to go.)

I know this is a thing that people do -- in my experience, most standalone Python apps package a Python installation within them. But I can't figure out how to get it to work for me.

I have copied Python.framework into my project using the Build Phases "Copy Files" panel, and Python.framework does show up in my app bundle, but I can't figure out how to get the application to reference it.

If I link to Python.framework in "Linked Frameworks and Libraries" in Xcode, the app builds properly, but fails on launch on 10.6 with the error

Dyld Error Message:
    Library not loaded: /System/Library/Frameworks/Python.framework/Versions/2.7/Python
    Referenced from: {/path/to/my/app}
    Reason: image not found

...because 10.6 only has Python 2.5, and it's searching for the system framework, not the one included in my app bundle.

If I don't link to the Python framework in Xcode, the app fails to build.

So how do I tell my application that I want it to use the Python.framework bundled inside my app at appname.app/Contents/Frameworks/Python.framework, and NOT the system Python.framework located at /System/Library/Frameworks/Python.framework?

I'm at a bit of an impasse with this issue, and any help would be greatly appreciated.

Community
  • 1
  • 1
ekl
  • 1,052
  • 2
  • 9
  • 24
  • have you tried using [py2app](https://pypi.python.org/pypi/py2app/)? – vik Jul 29 '13 at 19:42
  • I have, and packaging the app with py2app worked beautifully, but I was having a separate set of issues with the functioning of the app. (My app is intended to run as a background app, but I was having problems where it wouldn't start functioning until brought to the foreground once.) Most of the threads I encountered seemed to suggest that packaging the app through Xcode was the "proper" way to do it, so that's what I decided to try. But maybe that was bad advice. – ekl Jul 29 '13 at 19:52
  • 10.6? Do you expect many users to have Snow Leopard? – Yusuf X Jul 29 '13 at 21:14
  • i have written background osx apps before using py2app and they worked fine. There's an option called LSUIElement I believe, that if you set to True, it will eliminate the dock icon. and a decent amount of people still use 10.6 and it's still supported by Apple – vik Jul 29 '13 at 21:58
  • @YusufX I'd like to support 10.6 if possible. It's only two years old. – ekl Jul 29 '13 at 22:57
  • @vik My issue is that I'm using PyObjC, which creates its own Objective-C runloop and introduces additional complications. – ekl Jul 29 '13 at 22:59
  • parts of my code involved pyobjc, i'm no expert though, as i never tried to do it with xcode. Just used interface builder and used pyobjc code to interact with it – vik Jul 30 '13 at 00:23

1 Answers1

4

Don't try to copy /System/Library/Framework/Python.framework into your application bundle, that almost certainly won't do what you want (the application won't work on older OSX releases, and isn't necessary on the same or newer OSX releases), and is dodgy license wise.

To use the framework you need to rewrite the load commands, either by using install_name_tool, or by creating a custom python framework that already has the correct value for the install name (something like @executable_path/../../Framework/Python.framework/Python.

I usually use py2app to create the application bundle (not unsurprisingly, as I'm the maintainer for py2app), that will automaticly include the required Python dependencies as well.

Ronald Oussoren
  • 2,715
  • 20
  • 29
  • I think this is an issue for me. It's very specifically NOT bundling Python, actually its not even bundling python at all, it screws up the path settings and I can't run it without overriding it to my system python. Could you explain what @executable_path is, and when its expanded. Also, why it's not putting ANY frameworks in /Contents/Frameworks? – Ezekiel Jan 19 '15 at 15:31
  • @executable_path is a special path in dyld load commands and is expanded at load time to the path of the binary (in an .app that's the main binary in MyApp.app/Contents/MacOS). – Ronald Oussoren Jan 27 '15 at 16:23