2009
09.07

If you’ve tried to create a cocoa-python XCode project in Snow Leopard, you’ve probably realised that the templates for creating Python projects in XCode have not been included. The reason given for removing the Python (and Ruby) project templates from XCode was that the templates were changing faster than XCode updated, therefore they would become quickly outdated. The Cocoa-Python bridge however, is still fully supported, despite the missing templates. Python developers are now expected to obtain the templates directly from the PyObjC project site (pyobjc.sourceforge.net) and install them themselves. Having done that myself, so I thought I’d write a quick step by step guide that anybody can follow.

First, you need to download a copy of the templates. The most current version can be found in the subversion repository. The easiest way to obtain it is to open Terminal.app and type:

svn co http://svn.red-bean.com/pyobjc/trunk/pyobjc/pyobjc-xcode

You should now have a directory called pyobjc-xcode in your working directory. Type the following to change into the directory with the project templates:

cd pyobjc-xcode/Project\ Templates/

The project-tool.py script will assist with the installation of the templates. The README file contains the necessary commands to install the XCode project templates in the library folder of the current user (make sure they are written on a single line):

./project-tool.py -k -v --template Cocoa-Python\ Application/CocoaApp.xcodeproj/TemplateInfo.plist Cocoa-Python\ Application ~/Library/Application\ Support/Developer/Shared/Xcode/Project\ Templates/CocoaPython/Cocoa-Python\ Application
./project-tool.py -k -v --template Cocoa-Python\ Document-based\ Application/CocoaDocApp.xcodeproj/TemplateInfo.plist Cocoa-Python\ Document-based\ Application/ ~/Library/Application\ Support/Developer/Shared/Xcode/Project\ Templates/CocoaPython/Cocoa-Python\ Document-based\ Application
./project-tool.py -k -v --template Cocoa-Python\ Core\ Data\ Application/CocoaApp.xcodeproj/TemplateInfo.plist Cocoa-Python\ Core\ Data\ Application/ ~/Library/Application\ Support/Developer/Shared/Xcode/Project\ Templates/CocoaPython/Cocoa-Python\ Core\ Data\ Application
./project-tool.py -k -v --template Cocoa-Python\ Core\ Data\ Document-based\ Application/CocoaDocApp.xcodeproj/TemplateInfo.plist Cocoa-Python\ Core\ Data\ Document-based\ Application/ ~/Library/Application\ Support/Developer/Shared/Xcode/Project\ Templates/CocoaPython/Cocoa-Python\ Core\ Data\ Document-based\ Application

Once you restart XCode, you should see the four Cocoa-Python templates appear under “User Templates” like this:

XCode Python project templates

XCode Python project templates

The last step is to install the templates for creating individual Cocoa-Python files in XCode. The file templates are found in the same pyobjc-xcode directory we checked out of the svn repository earlier. In Terminal, go to the directory with the file templates (assuming you are still in the directory with the project templates as per the previous steps):

cd ../File\ Templates/Cocoa/

If it doesn’t already exist, create a File Templates directory in the current user’s library folder as follows:

mkdir ~/Library/Application\ Support/Developer/Shared/Xcode/File\ Templates

Copy all the file templates into that directory:

cp -r * ~/Library/Application\ Support/Developer/Shared/Xcode/File\ Templates/.

That’s it, now you should be able to create new Python files in XCode (see below). Enjoy PyObjC programming :-)

FileTemplates

43 comments so far

Add Your Comment
  1. Thanks.

  2. This line:

    ./project-tool.py

    Gives me

    ImportError: No module named Foundation

    Can someone help me, please? This is such bullsh*t. I’ve installed the Developer Tools from the Snow Leopard CD but it is still acting like I have no PyObjC installation. :(

    No one else I find trying to install the templates seems to be running into this problem.

  3. Hmm, something must have gone wrong, PyObjC should be installed with the developer tools. Are the rest of the developer tools installed properly? Without being able to see what’s gone wrong, the only things I can suggest are to either reinstall the developer tools or download PyObjC from the project website (http://pyobjc.sourceforge.net/) and install it manually. Sorry I can’t be of more help.

  4. I got the same error. Tried installing PyObjC and that generated some errors too:

    cc1: error: unrecognized command line option “-Wno-long-double”
    cc1: error: unrecognized command line option “-Wno-long-double”
    cc1: error: unrecognized command line option “-Wno-long-double”
    lipo: can’t open input file: /var/tmp//cccefy6P.out (No such file or directory)
    error: Setup script exited with error: command ‘gcc-4.2′ failed with exit status 1

    I have python 2.6 installed–could that be the problem?

  5. Sorry for the slow reply. I don’t think it’s a problem with Python, it’s a problem with the version of gcc that’s being used. Snow Leopard uses gcc 4.2 by default which doesn’t support the -Wno-long-double option. However, gcc 4.0 does support that option, and it should be installed in your system under /usr/bin/gcc-4.0. Before typing the command to install PyObjC, try setting the CC environment variable to gcc 4.0 by typing the following in the terminal:
    export CC=gcc-4.0
    Then type the command to install PyObjC (easy_install pyobjc) and hopefully it will build it without problems.

  6. Thank you! Been looking everywhere for an easy to follow tutorial.

  7. Thanks–no need to hurry. I appreciate your help.

    Unfortunately, it’s still not working. Generated a bunch of errors regarding “CGFloat”–mainly that it’s not available but it should be.

    Interestingly, when I run python2.5 from the command line I can successfully import the Foundation module. But when I run python2.6 (the default), it can’t.

    I did get the templates to show up in Xcode using commands like this:

    svn co http://svn.red-bean.com/pyobjc/trunk/pyobjc/pyobjc-xcode/Project%20Templates/Cocoa-Python%20Document-based%20Application/ /Developer/Library/Xcode/Project\ Templates/Application/Cocoa-Python\ NSDocument\ based\ Application

    However, creating and running a project with these templates causes some errors in assembly code. So something’s still not right. Too bad Apple didn’t just include these templates in Xcode–probably take them about 10 minutes to make it work for everybody.

  8. Hmmm, it sounds like PyObjC is installed for Python 2.5 on your machine but not for 2.6? Did you do a clean install of Snow Leopard or an upgrade? I did a clean install on my machine, and the PyObjC stuff is all installed properly for Python 2.6. Perhaps doing an upgrade somehow doesn’t install it properly?
    Have a look in /Library/Python/2.5/site-packages/ and see if there’s a .egg file that starts with pyobjc. If there is, perhaps you could copy that .egg file across to /Library/Python/2.6/site-packages/ and see if that allows you to run code that imports the Foundation module in python 2.6.
    Not sure if that would work, I’m just guessing here since I don’t have that problem.
    And yes, I agree it would have been very nice if Apple had included all the templates like it did in Leopard.

  9. I did an update install of Snow Leopard. I have a mess I think (even before I upgraded)–I have both the Apple python (2.6.1) and the MacPython (2.6.4) which I studiously installed as per instructions at sourceforge (this was prior to my upgrade to Snow Leopard).

    I can’t find that egg file.

    I’m going to pack it in for now. The python GUI I use at work is the wxPython package which I use because I need it to work on Windows too. I was really just curious to see what this PyObjC stuff was all about.

    I think it’ll have to wait until I have the time to clean off the MacPython and sort of start all over with just the Apple Python (or maybe just do a clean install). Either way it’s time consuming. Maybe I’ll get lucky and Apple will relent and put the Python stuff back into Xcode for me.

    I do have one parting question if you happen to know: Exactly where is the PYTHONPATH variable set? There must be some file somewhere, but I can’t find it.

    Thanks for your help. I learned some new stuff.

  10. If you do a clean install of snow leopard then you shouldn’t have any issues with PyObjC apart from installing the templates in XCode. You can do that quite quickly by backing up all your stuff using time machine, then do a clean install and tell it to restore everything from time machine. I installed snow leopard that way and it was all up and running again very quickly.
    As for PYTHONPATH, I’m not sure where it is set centrally, but I know you can just add paths to it from the command line by typing:
    export PYTHONPATH="$PYTHONPATH:/first/added/path:/second/added/path"
    If you want to make that change permanent then you just need to add that line to your .profile file in your home directory. Hope this helps.

  11. Okay, I think I’ll do that. Thanks!

  12. Great ! Thank you !

  13. This was a very helpful and clearly explained guide. I especially appreciated the Terminal commands, which worked with a cut and paste.

    Thanks a bunch!

    By the way, you have “easyinstall pyobjc” written above in a comment. I believe it’s “easy_install”.

  14. You are right about easy_install, it has been amended now. I’m very glad the guide was useful!

  15. Hi– When I try to use project-tool.py i get the following error:

    Traceback (most recent call last):
    File “./project-tool.py”, line 22, in
    from Foundation import NSDictionary
    File “/Library/Python/2.6/site-packages/pyobjc_framework_Cocoa-2.2b1-py2.6-macosx-10.6-universal.egg/Foundation/__init__.py”, line 10, in
    from CoreFoundation import *
    File “/Library/Python/2.6/site-packages/pyobjc_framework_Cocoa-2.2b1-py2.6-macosx-10.6-universal.egg/CoreFoundation/__init__.py”, line 17, in
    scan_classes=False)
    File “/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC/objc/_bridgesupport.py”, line 147, in initFrameworkWrapper
    _parseBridgeSupport(data, globals, frameworkName, inlineTab=inlineTab)
    File “/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC/objc/_bridgesupport.py”, line 58, in _parseBridgeSupport
    objc.parseBridgeSupport(data, globals, frameworkName, *args, **kwds)
    SyntaxError: Syntax error for struct CFSocketSignature

    Not sure exactly what the problem is here. I have a couple versions of python on my mac. Specifically I have an enthought build, and the 2.6 version that snow leopard came with.

    Additionally when I type: easy_install pyobjc, I get the following errors;
    easy_install pyobjc
    Searching for pyobjc
    Best match: pyobjc 2.2b2
    Processing pyobjc-2.2b2-py2.6.egg
    Removing pyobjc 2.2b1 from easy-install.pth file
    Adding pyobjc 2.2b2 to easy-install.pth file

    Using /Library/Python/2.6/site-packages/pyobjc-2.2b2-py2.6.egg
    Processing dependencies for pyobjc
    Searching for pyobjc-framework-Quartz==2.2b2
    Reading http://pypi.python.org/simple/pyobjc-framework-Quartz/
    Reading http://pyobjc.sourceforge.net
    Best match: pyobjc-framework-Quartz 2.2b2
    Downloading http://pypi.python.org/packages/source/p/pyobjc-framework-Quartz/pyobjc-framework-Quartz-2.2b2.tar.gz#md5=19fd563656931664acdce8296bd4844f
    Processing pyobjc-framework-Quartz-2.2b2.tar.gz
    Running pyobjc-framework-Quartz-2.2b2/setup.py -q bdist_egg –dist-dir /var/folders/Ff/FfPBJXLMEGqm9Ap+wgOPeU+++TI/-Tmp-/easy_install-ynYzQ2/pyobjc-framework-Quartz-2.2b2/egg-dist-tmp-9VHJPa
    /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/distutils/dist.py:266: UserWarning: Unknown distribution option: ‘options’
    warnings.warn(msg)
    In file included from Modules/_CVPixelBuffer.m:5:
    Modules/pyobjc-api.h:27:3: error: #error “Huh? 64-bit but no CFFloat available???”
    In file included from //System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreGraphics.framework/Headers/CGGeometry.h:8,
    from //System/Library/Frameworks/ApplicationServices.framework/Frameworks/ATS.framework/Headers/ATSTypes.h:32,
    from //System/Library/Frameworks/ApplicationServices.framework/Frameworks/ATS.framework/Headers/ATSLayoutTypes.h:36,
    from //System/Library/Frameworks/ApplicationServices.framework/Frameworks/ATS.framework/Headers/ATS.h:33,
    from //System/Library/Frameworks/ApplicationServices.framework/Headers/ApplicationServices.h:26,
    from //System/Library/Frameworks/CoreVideo.framework/Headers/CVDisplayLink.h:24,
    from //System/Library/Frameworks/CoreVideo.framework/Headers/CoreVideo.h:24,
    from Modules/_CVPixelBuffer.m:9:
    //System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreGraphics.framework/Headers/CGBase.h:105: error: conflicting types for ‘CGFloat’
    Modules/pyobjc-api.h:30: error: previous declaration of ‘CGFloat’ was here
    lipo: can’t open input file: /var/folders/Ff/FfPBJXLMEGqm9Ap+wgOPeU+++TI/-Tmp-//cc1xdVE8.out (No such file or directory)
    error: Setup script exited with error: command ‘gcc-4.2′ failed with exit status 1

    Thank you in advance for any thoughts or ideas that you have that might help me in using pyobjc templates in xcode

  16. Hi–

    I have followed the instructions here, but could only get the project-tool.py to work properly by using it with python2.5. (The enthought edition). Additionally, I can see that the templates are available. My next problem is that I get the build error:

    ld: warning: in /Developer/SDKs/MacOSX10.6.sdk/Library/Frameworks//Python.framework/Python, file is not of required architecture.

    I am not really sure what this means. Is it a question of 32bit vs 64 bit architecture? And is there a simple way to fix?

    Thanks for your help in advance, David

  17. I’m not sure what the architecture issue is. Could it be a powerpc version of Python or something? I don’t think it’s a 32bit vs 64bit issue because as far as I know Snow Leopard should be able to deal with that. Have you tried getting the latest Python 2.6 and see if it works with that? Sorry I can’t be of more help, I’m not able to replicate this issue on my machine so I’m not sure what’s wrong.

  18. thanks for sharing. i love to copy & paste instead of reading README.txts :)

  19. Thanks for sharing!

    There was one thing I had to change from your recipe to have the file templates working: create another subdirectory called “Python” under “File Templates” to hold the python-related templates.

    > cp -r * ~/Library/Application\ Support/Developer/Shared/Xcode/File\ Templates/Python

    # Xcode 3.2.1, Mac OS X 10.6.2

  20. [...] O primeiro problema que eu resolvi relativamente r

  21. Wow. You’re smart, hot, and you can use the heck out of a Mac. I’m in love.

    No, seriously, this rocks. I had some trouble getting it to work at first but using your instructions, I was able to backtrack the issue and get them all installed. Thanks! You rule!

  22. [...] Fonte 1. [...]

  23. Thankyou one million times, I have been awake for 36 hours wrapping round on red bull and there’s no way I would have been able to do that without your blogpost. Now to wind down with some light python coding :-)

  24. thankyou so much brother..

  25. Thanks for a good guide. I noticed that you’re not allowed to have non-ascii characters in the .py files comments, which was the ‘default’ for me since my name contains an

  26. Thank you!

  27. Thanks a lot for pointing out and providing ready to paste scripts. Saves me a lot of time.

  28. [...]

  29. thanks for the tutorial but the Python subclasses in my xcode don’t have the same icons as yours. Actually they have the same icon of a empty file.
    where should I change that?

  30. Thanks a lot. The install process worked seamlessly for me on the latest Snow Leopard and xCode tools versions.

  31. same here, thanks a lot.

  32. Great post! Very useful! Thanks a bunch.

  33. Finally everything is working, but i had to install everything as root, the problem now is that xcode show me the cocoa-python icons fine but just when i run it as root, not as a normal user.
    Other thing is i can import the pyobjc just as root, not as a user.
    There is a way to have the same sys.path that root? i think that my problem.

  34. Could it be an issue with permissions? Since you installed it as root, maybe the permissions are set such that only root can read the files. It might be worth checking that. Also check that the templates have been copied to the correct folders:
    ~/Library/Application Support/Developer/Shared/Xcode/File Templates
    and
    ~/Library/Application Support/Developer/Shared/Xcode/Project Templates

  35. Awesome Tut! Thanks a lot!!!

  36. Very useful! Thanks!

  37. [...] インストールするためには,Ioanna's digital perchさんの記事「Installing PyObjC XCode templates in Snow Leopard」に書いてあるコマンドをそのまま実行すればオーケーです.元の記事は英語なので,日本語で少しコマンドの解説を書いてみます. [...]

  38. @DavidG and credford:
    I found that you must enter

    export PYTHONPATH=”/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/”

    right before the first ./project-tool.py line.
    That should work.

  39. Hi,

    I installed pyobjc using easy_install but nothing changes on XCode.
    When I click in new project I’m not able to see “User Templates” Just iOS and MacOSX Templates.

    Any tip of that?
    I try to follow the steps of the post, but nothing changes too.

    Really need this working to use Xcode to code in Python.
    Thanks in advance.

  40. I’m using Mac OS Lion 10.7.1
    Xcode 4.1 Build Version 4B110
    Python 2.7.1

  41. My instructions were for Snow Leopard and XCode 3.x. I haven’t tried using PyObjC in Lion with XCode 4 so I can’t help you there, sorry.

  42. One of the best tutorials out there. Perfect.

  43. Thanks…it worked great :)