views:

478

answers:

1

I want to get started trying to develop a few simple applications with PyObjC. I installed PyObjC and the Xcode templates. I know that PyObjC itself works, since I've run this script successfully. When I tried to create a project from the Cocoa-Python Application template and ran it, I got this error:

Traceback (most recent call last):
  File "main.py", line 10, in 
    import objc
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyObjC/objc/__init__.py", line 25, in 
    from _convenience import *
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyObjC/objc/_convenience.py", line 21, in 
    from itertools import imap
ImportError: dlopen(/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload/itertools.so, 2): no suitable image found.  Did find:
    /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload/itertools.so: mach-o, but wrong architecture
2010-02-08 19:40:09.280 TestApplication[3229:a0f] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '/Users/icktoofay/Desktop/TestApplication/main.m:44 main() PyRun_SimpleFile failed with file '/Users/icktoofay/Desktop/TestApplication/build/Debug/TestApplication.app/Contents/Resources/main.py'.  See console for errors.'

When I tried opening a normal Python prompt and importing itertools, there was no error. I'm using Python 2.6.4 from MacPorts on Mac OS X 10.6 Snow Leopard.

I'd appreciate any help.

+3  A: 

You have a 32-bit vs 64-bit problem. It appears you are using a Python 2.6 installed from MacPorts and apparently it was not a universal (32-bit/64-bit) build. Either your app is running as 64-bit and the Python is only 32-bit or the reverse. You can check by using file:

cd /opt/local/Library/Frameworks/Python.framework/Versions/2.6/
cd lib/python2.6/lib-dynload/
file itertools.so 
itertools.so: Mach-O universal binary with 2 architectures
itertools.so (for architecture x86_64): Mach-O 64-bit bundle x86_64
itertools.so (for architecture i386):   Mach-O bundle i386

The easiest fix is likely to re-install the MacPorts Python and the additional packages you installed like PyObjC:

sudo port selfupdate
sudo port -u install python26 +universal ...

EDIT: Since you report that the Python is 64-bit, the problem then is almost certainly due to a problem with the Xcode template setup for your Python PyObjC project. The startup code is probably loading the Apple-supplied Python interpreter which is universal. You can check by adding something like this prior to the import objc:

import sys
sys.stderr.write(sys.executable)

For MacPorts, it should be

/opt/local/Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python

I'm not familiar enough with the ins-and-outs of using the templates under Xcode to know what might need to be changed and I doubt that many people use them with a MacPorts Python, especially under 10.6.

Another thought, the Apple-suppied Python 2.6.1 comes with a version of PyObjC already installed. Perhaps using it would be simpler. Or don't use Xcode and use py2app or another solution to run it.

Ned Deily
I checked the architectures on itertools.so, and it looks like it's 64 bit: `itertools.so: Mach-O 64-bit bundle x86_64` When I tried this on the Python executable, it was also 64-bit: `python: Mach-O 64-bit executable x86_64`
icktoofay
See EDIT for additional suggestions.
Ned Deily
The reason I'm using a non-Apple Python is because 2.6.1 had a bug which caused a segmentation fault in a couple of my scripts, so I had to upgrade, and Apple only really updates their Python versions when they release a new OS version. Also, PIL is somewhat of an annoyance to get working with the Python.org Python, so I just opted with the MacPorts `py26-pil` package, which had everything I needed...until this point, at which I discover that it has issues with PyObjC and Xcode.
icktoofay
Additionally, `py26-pil` doesn't want to install anymore. (no idea why) The log is here, if you're interested: http://pastebin.com/d784f4d13
icktoofay
Well, since Xcode and PyObjC work now, (no idea why), I'm going to mark your answer as the answer... but PIL doesn't work now. I guess I'll have to find another way to get all of these things working at once.
icktoofay
MacPorts PIL works for me. Sorry, I don't have time to analyze the problem in detail but I suspect it's a dependency issue with universal vs already non-universal. If you don't mind redoing it, suggest you edit your `/opt/local/etc/macports/variants.conf` file to include `+universal` and possibly `+no_x11 +no_tkinter`, uninstalling your existing MacPorts packages with `sudo port uninstall installed` and starting over. You should end up with a working PIL.
Ned Deily