Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Daspeed

macrumors newbie
Original poster
Aug 17, 2006
28
0
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:

[Session started at 2007-10-29 23:26:49 +0100.]
Traceback (most recent call last):
File "/Users/Dan/PythonTest/build/Release/PythonTest.app/Contents/Resources/main.py", line 2, in <module>
import objc
ImportError: No module named objc

2007-10-29 23:26:49.107 PythonTest[2735:10b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '/Users/Dan/PythonTest/main.m:44 main() PyRun_SimpleFile failed with file '/Users/Dan/PythonTest/build/Release/PythonTest.app/Contents/Resources/main.py'. See console for errors.'
2007-10-29 23:26:49.108 PythonTest[2735:10b] Stack: (
2517521003,
2482397339,
2517520459,
2517520522
)
Loading program into debugger…
GNU gdb 6.3.50-20050815 (Apple version gdb-768) (Tue Oct 2 04:07:49 UTC 2007)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".Program loaded.
sharedlibrary apply-load-rules all
Attaching to program: `/Users/Dan/PythonTest/build/Release/PythonTest.app/Contents/MacOS/PythonTest', process 2735.
(gdb)

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?
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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:

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.
 

Daspeed

macrumors newbie
Original poster
Aug 17, 2006
28
0
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.
 

Daspeed

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

LaskoVortex

macrumors newbie
Jan 15, 2008
13
0
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.
 

mcow

macrumors newbie
Feb 16, 2008
2
0
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:

Code:
int main(int argc, char *argv[])
{
[INDENT]
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);
[/INDENT]
...

Replace this line:
Code:
NSArray *pythonPathArray = [NSArray arrayWithObjects: resourcePath, [resourcePath stringByAppendingPathComponent:@"PyObjC"], nil];

with this:
Code:
    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.
 

hhas

macrumors regular
Oct 15, 2007
126
0
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).

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.
 

GirthP

macrumors regular
Oct 1, 2007
121
0
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:

Code:
int main(int argc, char *argv[])
{
[INDENT]
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);
[/INDENT]
...

Replace this line:
Code:
NSArray *pythonPathArray = [NSArray arrayWithObjects: resourcePath, [resourcePath stringByAppendingPathComponent:@"PyObjC"], nil];

with this:
Code:
    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.


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!
 

mcow

macrumors newbie
Feb 16, 2008
2
0
Will I have to do this with each project??

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

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?

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).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.