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.

9 comments:

Dixon said...

pep8.py not available.
Can you attach it to your post?

Guillaume Chéreau said...

Dixon : you can find a copy of the script here.

Dixon said...

Thank you very match!!
You made me happy!! :)

Dixon said...

>coding style error will be highlighted on the fly

I made the same. I coding style error will be highlighted, but I don't see simple coding error (undefined variables, etc).

Do you see coding error?

Dixon said...

Exactly, I use pyflakes in python mode:



(when (load "flymake" t)
(defun flymake-pyflakes-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 "pyflakes" (list local-file))))

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



And pyflakes and pep8.py don't work together.

Guillaume Chéreau said...

It could be possible to use pyflake and PEP8.py at the same time by concatenating the output from both programs. Too bad pyflake (or pylint) both don't allow to check the codding style.

Dixon said...

I have solution!

http://stackoverflow.com/questions/1259873/how-can-i-use-emacs-flymake-mode-for-python-with-pyflakes-and-pylint-checking-cod

erectile dysfunction remedies said...

Wow, incredible blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your site is great, let alone the content!

erectile said...

If some one wishes expert view about blogging and site-building then i suggest him/her to pay a visit this website, Keep up the pleasant work.