Monday, 8 November 2010

Taming the Komodo dragon for virtualenv and python

My first python editor on osx was textmate. Like many others I love it's simplicity, bundles, and the fact that it actually feels like an osx app and not some OS bastard love child, but after a while I found I really wanted seamless autocomplete and calltips which I'd always had on Windows. Every year I look in vain at the macromates site for textmate 2, and every year I become increasingly convinced Duke Nukem Forever will ship first.

Although I still use textmate for quick edits I moved on to Komodo IDE some time ago. I'm not hardcore enough for vim, emacs, ed, cat, or butterflies and it did most of what I wanted it to do; autocomplete. calltips, mercurial, and git integration etc. Its biggest shortcoming though is that it doesn't support different interpreters per project a la virtualenv so you end up switching between the osx terminal and Komodo, and using it as a glorified text editor rather than an IDE. I was hoping that would be resolved in the newly released version 6 but alas no.

Since I mostly use it to do Django work, I flirted with Pycharm which does support virtualenv & Django but I just don't seem to enjoy Java based IDE's, so sorry Jetbrains, nothing personal, but it's not for me. I'll also be curious to try Xcode 4 when it releases to see how it shapes up for pythonistas, but for now it's Komodo or commandline (ipython/bpython etc).

Disappointment however is the mother of invention, and I belatedly worked out that actually you can use virtualenv with Komodo, just not the way you expect. Here's the trick if you haven't already worked it out:

In your Komodo preferences, make sure you set the Default Python Interpreter to Find on Path.






Also in the preferences, in Environment add /Users/$USER/bin to the front of your PATH, and add the PYTHONPATH variable set to $PWD and then create the bin directory in your home directory if you haven't already got one.
Virtualenvwrapper has hook files that allow you to customize virtualenv behaviour, at either global level in the $WORKON_HOME directory or per env in the $VIRTUAL_ENV bin directory. This is just what we need.

Normally I store my project files inside the virtualenv in a folder called project so I have a premkvirtualenv script with mkdir /$WORKON_HOME/$1/project to create the folder in the env automatically.

Next I edited the global postactivate script:

ln -sf $VIRTUAL_ENV/bin/python /Users/$USER/bin/python
cd $VIRTUAL_ENV/project

This ensures the virtual python we're working on it is always the first python to be found on the path when Komodo launches, and then it changes into the directory I want to be in the pythonpath.

You can then edit your virtualenvwrapper's postactivate script for the project in env's bin directory, so that it sets the Django settings modules and launches Komodo IDE when you run workon [your_env] from the terminal.

export DJANGO_SETTINGS_MODULE=your_project.settings
/Applications/Komodo\ IDE.app/Contents/MacOS/komodo-bin &>/dev/null &

Now any python interpreter's you start in Komodo have your Django settings ready to go and will use the correct python binary, and site-packages.

Simple huh? Wish I'd realised that a year ago...

Anyway, Komodo 6 looks pretty solid, and using the preferences can drop down to a pretty minimalist interface which I like. I look forward to digging around the improvements in the new version, and if you're not wedded to a particular python IDE you might want to give Komodo 6 IDE a whirl.

Hedged Down