pylint是python上面的lint, lint是古早用於C語言語法檢測的 工具, lint支援emacs..不僅如此lint支援emacs的flymake-mode.
flymake-mode是emacs上面相當強大的語法檢察工具, 他透過外部 程式進行檢查,然後依照檢查結果反應到編輯的buffer.
安裝
安裝pylint還挺煩的,照步驟慢慢來吧..
Download pylint.
Download logilab-astng
Download logilab-common
Download optik
將每個tarbal解壓縮,並進入解壓縮目錄執行:
python setup.py install
Note
如果不放心可以到各package的/test目錄下跑測試
- 建立一個python script 'epylint' (注意!!沒有副檔名py喔)
#!/usr/bin/env python
import re
import sys
from subprocess import *
p = Popen("pylint -f parseable -r n --disable-msg-cat=C,R %s" %
sys.argv[1], shell = True, stdout = PIPE).stdout
for line in p.readlines():
match = re.search("\\[([WE])(, (.+?))?\\]", line)
if match:
kind = match.group(1)
func = match.group(3)
if kind == "W":
msg = "Warning"
else:
msg = "Error"
if func:
line = re.sub("\\[([WE])(, (.+?))?\\]",
"%s (%s):" % (msg, func), line)
else:
line = re.sub("\\[([WE])?\\]", "%s:" % msg, line)
print line,
p.close()
- 將 'epylint' 丟進環境變數path的目錄中好讓flymake-mode可以收尋到.
- .emacs增加
(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 "epylint" (list local-file))))
(add-to-list 'flymake-allowed-file-name-masks
'("\\.py\\'" flymake-pylint-init)))
** 額外的keybind/和進入python mode 自動啟動參考
(add-hook 'python-mode-hook
(lambda ()
(flymake-mode)
(local-set-key (kbd "\C-c e")
'flymake-display-err-menu-for-current-line)
(local-set-key (kbd "\C-c `") 'flymake-goto-next-error)))
執行
進入python-mode 執行指令(假如你沒有設定自動啟動):
M-x flymake-mode
你會看到python script有藍色的代表警告,紅色代表錯誤.
想知道錯誤原因可以使用function:
M-x flymake-display-err-menu-for-current-line
沒有留言:
張貼留言