Monday, December 13, 2010

XCode again

I got a bug the other day about my Sudoku app (post here), so I thought I'd see if I could fix it. As it turns out, I'm rewriting it (sort of), but that's OK because it's Python and so it's easy. I followed very thorough instructions (here) to grab the templates for XCode from Ronald Oussoren's subversion thingie and install them properly. It turns out that post came just days after I whined about the issue (here, FWIW) and turned my back on PyObjC.

Now it's a year later and I can hardly remember anything (it really sucks having a birth year that has the same diff with this year as its last two digits; it's not a senior moment anymore but a senior epoch). I had a wee bit o'trouble hooking up my custom NSView subclass. It turns out that if you write the class (or at least stub it out) first, then Interface Builder will know about it and you can just drag a Custom View onto the nib window and set the class, and you're done.

So then, naturally, I tried it on my machine at work, and failed. And on my laptop, and failed as well! For a different reason.

And I decided to look at this as a learning opportunity. :)

In my brutish way, I added this to main.py:

import sys
print sys.version

And I get:

2.6.4 (r264:75821M, Oct 27 2009, 19:48:32) 
[GCC 4.0.1 (Apple Inc. build 5493)]
Traceback (most recent call last):
File "main.py", line 14, in
import objc
ImportError: No module named objc

That is definitely not the system Python. But the thing is, I have a lot of different Pythons on my machine (2.6.1, 2.6.4, 2.6.6, 2.7.1, 3.0)... These two are the same:

$ /usr/bin/python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin

$ python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin

which one is 2.6.4?
not the one from the other day:

$ ~/bin/python26
Python 2.6.6 (r266:84292, Dec 11 2010, 16:10:19)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin

it's not even the MacPorts one either, different build date:

$ /usr/local/bin/python
Python 2.6.4 (r264:75706, Jan 31 2010, 16:39:21)
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin

Using Unix find (here):

sudo find / -name python

some of these are directories, not executables

sudo find / -name python -exec ls -ald {} \;

More than 30---mostly from PyObjC projects built as standalones!
I found it (should've guessed):

$ /Library/Frameworks/Python.framework/Versions/2.6/bin/python
Python 2.6.4 (r264:75821M, Oct 27 2009, 19:48:32)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import objc
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named objc


I'm not sure, perhaps this was an aborted experiment with MacPython python...
So, to solve it, modify .pydistutils.cfg

[install]
install_lib = /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages
install_scripts = ~/bin



mv x.txt ~/.pydistutils.cfg
sudo /Library/Frameworks/Python.framework/Versions/2.6/bin/python -m easy_install pyobjc==2.2
..
$ /Library/Frameworks/Python.framework/Versions/2.6/bin/python
Python 2.6.4 (r264:75821M, Oct 27 2009, 19:48:32)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import objc
>>>

and the XCode project works! I asked a question on Stack Overflow about how to get XCode to do what I want it to do. And I'll let you know.

2 comments:

David said...

I appear to be having a similar problem. When I run python in my terminal, I see:

Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin

However, when Xcode builds the default PyObjc template project:

2.5.1 (r251:54869, Apr 18 2007, 22:08:04)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)]

It looks like it's getting this version from /Library/Frameworks/Python.framework/Versions/2.5/bin/python

Because when I run the above, and try `import objc` it fails.

In my Xcode project, the linked Python framework points to the /System location, which appears to be correct:

http://cl.ly/3X2m1G1b3x0f2S2i452R

Any help would be much appreciated! Thanks!

telliott99 said...

Yep. If you have easy_install on this one, you can fix it as described in the post. Of course, if you also have other Python installs, it might be one of them. And I haven't figured out yet how to point XCode in the right direction. I suspect it has something to do with the SDK that you're linking against.