OSX, Vim, and Python

I've been using python and vim for the past few years as my sole development environment since I hate GUIs, and I love how simple and powerful vim really is. I started off with a simple tutorial, and then expanded from there to develop my own custom IDE. I recently discovered that vim7 has support for running python commands inline. This only further's my love of vim.

I also recently discovered a nice little checker for vim called pyflakes. This program allows you to check python scripts for simple errors that are detected at compile time in languages that support that sort of thing. It includes being able to check for un-imported modules, typos, and even things that you imported that you didn't use within that module.

The problem is that I'm using Mac OSX, which comes with vim that isn't compiled with "--enable-pythoninterp", so the pyflakes vim plugin doesn't work. A simple workaround is on the tutorial page I found, by simply installing MacVim though you only get it compiled against python2.5, not 2.6 which is standard now in Snow Leopard. Additionally, the syntax highlighting in MacVim is horrible for python compared to the built in one that ships with OSX, and I still prefer to run vim in the console.

Solving this issue involves a few steps:

1) Build MacVim from source. Of course you'll need the 10.6 XCode to do this, but after that it's pretty straight forward. The only thing to watch is that you have to run "./configure --enable-pythoninterp --with-macsdk=10.6" instead of just "./configure". Other then that you can follow the instructions from the MacVim site for compiling your own version.

2) Copy the MacVim.app folder into your /Applications folder

3) add [ -x "/Applications/MacVim.app/Contents/MacOS/Vim" ] && alias vim=/Applications/MacVim.app/Contents/MacOS/Vim To your shell rc (for me it's .zshrc but for most people it's probably .tcshrc). I noticed that simply copying the Vim binary to your PATH, or sym linking it doesn't work, not quite sure why but this works well enough for me.

4) Copy the python syntax file from /usr/share/vim/vim72/syntax/python.vim to ~/.vim/syntax/python.vim

5) Download and install the pyflakes.vim plugin

6) Open up a new terminal and edit an already created python source file.

After you've done all of that you should see that anything that would normally show up in an error while running pyflakes now automatically get's highlighted as an error.

I've also been using the pyflakes binary included with the pyflakes.vim plugin to check everything in my modules before building and uploading them to our servers. The only issue I've noticed is that sometimes you need to import a module even though you don't need to use it (for example with boto this is used in the sdb.db module to enable reverse-references). I'm still not quite sure how to ignore those or get around that.

One other thing that doesn't work is doing conditional imports, for example to get JSON support the manuals tell you to do:

   import json
except ImportError:
   import simplejson as json

But in pyflakes, this will result in an error.