Friday, October 13, 2006

Python and Xcode

I've been doing a lot of very productive python work recently and have been looking around for the best programmer's editor but still haven't found it.

If you spend you whole day in an application you do start to think about how it could be made more productive, and of course anyone who programs can't help but customise.
Features I'm after are:
  • Syntax colouring
  • Syntax aware (auto indenting etc)
  • Real code completion (I mean language aware like looking into imported modules for possible completions)
  • Ease of showing documentation on a module and it's methods
  • Code folding might be nice
  • A full gui debugger might be nice
  • Native MacOS X application
  • Handles large projects fast
  • Subversion GUI
TextMate blew me away and was the first editor to get me off a long term addiction to BBedit, but I got a bit dissatisfied with how long it takes to check all the files in a large project when I switch back to it. (I know you can turn this off but that would be a pain too).


I've used XCode of course for Cocoa development and it has python syntax colouring built in. XCode is particularly good with large numbers of source files, it's fast and searching is really great. There's a little Script menu I hadn't played with before now and I want to share what I've discovered with you.

User scripts for that little menu go in ~/Library/Application Support/Apple/Developer Tools/Scripts/ (insane eh?).

Inside that Scripts directory you should copy the default StartupScript and 10-Users Scripts folder from /Library/Application Support/Apple/Developer Tools/Scripts/

Here are my scripts, mostly adapted from the excellent ones that come with TextMate.

#! /bin/bash
# -- PB User Script Info --
# %%%{PBXName=PyChecker}%%%
# %%%{PBXKeyEquivalent=}%%%
# %%%{PBXInput=None}%%%
# %%%{PBXOutput=None}%%%

${PYCHECKER} --only %%%{PBXFilePath}%%% > ${TEMPOUT}
open -a /Developer/Applications/ ${TEMPOUT}

#! /bin/bash
# -- PB User Script Info --
# %%%{PBXName=Python Run...}%%%
# %%%{PBXKeyEquivalent=}%%%
# %%%{PBXInput=None}%%%
# %%%{PBXOutput=SeparateWindow}%%%

echo "Running %%%{PBXFilePath}%%%..." >${TEMPOUT}
${PYTHON} %%%{PBXFilePath}%%% >> ${TEMPOUT}

open -a /Developer/Applications/ ${TEMPOUT}

#! /bin/bash
# -- PB User Script Info --
# %%%{PBXName=pydoc}%%%
# %%%{PBXKeyEquivalent=}%%%
# %%%{PBXInput=Selection}%%%
# %%%{PBXOutput=None}%%%
pydoc -k %%%{PBXSelectedText}%%%

# This command takes the currently selected word and
# displays the python documentation for the module
# corresponding to said word.
# It falls back on the current word.

# change to /tmp to avoid possibly overwriting
# an html file in the working directory.


cd /tmp

${PYDOC} -w "%%%{PBXSelectedText}%%%" >/dev/null
if [[ -f "%%%{PBXSelectedText}%%%.html" ]]; then
open "%%%{PBXSelectedText}%%%.html"
#rm -f "%%%{PBXSelectedText}%%%.html"
echo "

No documentation found for:


This command only looks for Python modules."


#! /bin/bash
# -- PB User Script Info --
# %%%{PBXName=Python Reference}%%%
# %%%{PBXKeyEquivalent=}%%%
# %%%{PBXInput=None}%%%
# %%%{PBXOutput=None}%%%


Bill said...

Not to beat a horse that is dead all over the internet, but Vim has everything you list, except being a native OSX application.

Aron said...

Komodo? or is that too slow for you?

nyenyec said...

Eclipse with PyDev.

Yair said...

more open-source options:
* smultron
* the idle variant from has smart completions

commercial options:
* wing-ide

ogghead said...

Komodo for Mac OS X is actually pretty snappy (the x86 4.0beta version, anyway).

It's a great editor / debugger, and I love that I can use it on Windoze and Linux as well.

marxy said...

Thanks for the comments folks. I've tried Komodo, Eclipse with PyDev and Wing in the past but for some reason I feel distanced from the machine if I'm not running a native text editor.

The best experience I've had with an IDE was CodeWarrior developing in PowerPlant. It was so fast and productive.

Ben said...

eric3 runs on os x, although xcode is so hosed its cool. Assuming you don't have ubuntu os x should suffice for baby apps if you like to break your own mind repeatedly.

michaelian said...

you can beat a dead horse in the mouth but you can't make him drink. Vim is a native osx application.

VIM - Vi IMproved 6.2 (2003 Jun 1, compiled Feb 16 2006 18:28:54)
Compiled by

Anonymous said...

Thanks for the scripts! Xcode is the way to go on Macs.