Friday 7 August 2009

Check python coding style on the fly with emacs


A nice emacs trick : using flymake-mode and this python code styles PEP8 checker script written by Johann C. Rocholl, we can have automatic real time checking for standard python coding style.

To make this work I copied the script (pep8.py) in my PATH, and then I added this block of code in my .emac file :

(when (load "flymake" t)
(defun flymake-pylint-init ()
(let* ((temp-file (flymake-init-create-temp-buffer-copy
'flymake-create-temp-inplace))
(local-file (file-relative-name
temp-file
(file-name-directory buffer-file-name))))
(list "pep8.py" (list "--repeat" local-file))))

(add-to-list 'flymake-allowed-file-name-masks
'("\\.py\\'" flymake-pylint-init)))

After this I can just enable flymake mode when I edit a python file, and every coding style error will be highlighted on the fly.