Thursday, January 26, 2012

Fwd: Taming the Komodo dragon for virtualenv and python http://blog.haydon.id.au/2010/11/taming-komodo-dragon-for-virtualenv-and.html

http://blog.haydon.id.au/2010/11/taming-komodo-dragon-for-virtualenv-and.html

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 PYTHONPATHvariable set to $PWD and then create the bindirectory 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.

Posted by at 3:27 AM
Labels: 

4 comments:

Doug Hellmann said...

That's pretty nice. You should package it up as a plugin, like I did with virtualenvwrapper-emacs-desktop (http://www.doughellmann.com/projects/virtualenvwrapper-emacs-desktop/).

8 November 2010 11:02 AM
Daniel Apolinario said...

I use virtualenv differently. Working with and he uses Pinax virtualenv.

What I did was change the import directory in the project. Also create some functions in the toolbox to use python Pinax (which is in virtualenv).

In my toolbox I have a folder to work only with pure Django and another folder to work with Pinax.

9 November 2010 6:01 AM
Brett said...

@doug thanks for the suggestion (and virtualenvwrapper!). I'm not sure in itself it would add much value, or maybe I'm not seeing it clearly.

People like to install their project, working directory, and envs in all sorts of places, and also set their django settings to different contexts.

If I'm pre-defining their layout and settings then I'm moving down the buildout path of one "correct" way. Not sure if that works. 

It would be so much simpler if Komodo/other editors just allowed environment variables per project, though I appreciate that is easier said than done.

What I would like to see though is a one step install packages in new environment or even the reverse, make new env and install:

pip install -c 

mkvirtualenv --install or -r

10 November 2010 1:15 PM
Eric Promislow said...

Version 7 of Komodo supports some project-specific preferences, notably Python projects. The environment settings are still global only.

- Eric

23 November 2011 6:19 AM

No comments:

Post a Comment