Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 29, 2007, 05:36 PM   #1
Daspeed
macrumors newbie
 
Join Date: Aug 2006
Xcode 3 Cocoa-Python Application

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:

Quote:
[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?
Daspeed is offline   0 Reply With Quote
Old Oct 29, 2007, 05:44 PM   #2
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Quote:
Originally Posted by Daspeed View Post
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.
kainjow is offline   0 Reply With Quote
Old Oct 30, 2007, 03:34 AM   #3
Daspeed
Thread Starter
macrumors newbie
 
Join Date: Aug 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.
Daspeed is offline   0 Reply With Quote
Old Oct 30, 2007, 02:01 PM   #4
Daspeed
Thread Starter
macrumors newbie
 
Join Date: Aug 2006
After removing Python from the Frameworks folder and re-installing Xcode, it seems to work. For now at least...
Daspeed is offline   0 Reply With Quote
Old Jan 15, 2008, 01:39 PM   #5
LaskoVortex
macrumors newbie
 
Join Date: Jan 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.
LaskoVortex is offline   0 Reply With Quote
Old Feb 16, 2008, 08:20 PM   #6
mcow
macrumors newbie
 
Join Date: Feb 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:

Code:
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:
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.
mcow is offline   0 Reply With Quote
Old Feb 17, 2008, 07:36 AM   #7
hhas
macrumors regular
 
Join Date: Oct 2007
Quote:
Originally Posted by mcow View Post
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.
__________________
Learn AppleScript, 3rd edition, Sanderson & Rosenthal:
http://www.apress.com/9781430223610
hhas is offline   0 Reply With Quote
Old Mar 5, 2008, 06:54 AM   #8
GirthP
macrumors regular
 
Join Date: Oct 2007
Quote:
Originally Posted by mcow View Post
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[])
{
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:
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!
__________________
iPhone 16GB 3G
Home: 2.2 MBP 4GB 320GB WD Scorpio

Work: 8 core 2.8 MP 8GB w/ 2 24" Dell UltraSharps
GirthP is offline   0 Reply With Quote
Old Mar 6, 2008, 06:32 PM   #9
mcow
macrumors newbie
 
Join Date: Feb 2008
Quote:
Originally Posted by GirthP View Post
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/".

Quote:
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).
mcow is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Xcode or cocoa? Pajamaxllama iPhone/iPad Programming 9 Oct 22, 2013 09:01 AM
XCode - Cocoa-applescript question djjasonsa Mac Programming 3 Feb 21, 2013 03:50 PM
Saving a cocoa application to like an executable. RookieAppler Mac Programming 16 Feb 11, 2013 11:02 AM
Need Assitance in creating a simple Cocoa OS X application lilabila Mac Programming 2 Aug 18, 2012 12:52 PM

Forum Jump

All times are GMT -5. The time now is 11:03 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC