牌語備忘録 -pygo

あくまでもメモです。なるべくオフィシャルの情報を参照してください。

牌語備忘録 -pygo

Emacs の Flymake で、 Python の PEP8 の構文チェックがうまくいかない場合のメモ

下記参考サイトのようにやっても、PEP8のチェックがうまく動かなかったのでメモ。

(環境: MacOSX10.7, Emacs23.3.1, Python2.7)

EmacsWikiに載ってたものをちょいといじってみた

lintrunner.py を使ってやってみた。

Newest version of this code from the original author here.

EmacsWiki: Python Programming In Emacs

下準備、インストールなど

MacPortsでインストール
  • python27
  • py27-pyflakes
    • command = pyflakes-2.7
  • py27-pip
    • command = pip-2.7
pipでインストール
  • pep8
    • command = pep8.py

『lintrunner.py』をダウンロードして若干いじる

『/opt/local/bin/』あたりに置いて。実行権限を与える。

line:12
PEP8_COMMAND = "pep8.py"
PYFLAKES_COMMAND = "pyflakes-2.7"
line:241

Pep8とPyflakesだけ使うので(CompilerRunnerはいるのか?)

    for runnerclass in (# PylintRunner,
                        # PycheckerRunner,
                        Pep8Runner,
                        PyflakesRunner,
                        CompilerRunner):
class Pep8Runner*1

日本語とか1文字=2文字とカウントするらしく、コメント書いたりするとすぐ『E501: line too long』と1行80文字オーバーのエラーが出る。
なのでE501は無視する。

class Pep8Runner(LintRunner):
:
    sane_default_ignore_codes = set([
            "E501", #line too long
            ])
:
    @property
    def run_flags(self):
        # return '--repeat', '--ignore=' + ','.join(self.ignore_codes)
        return '--repeat', '--ignore=' + ','.join(self.operative_ignore_codes)

init.el

(when (load "flymake" t)

  ;; FlymakePython
  (defun flymake-pyflakes-init ()
     (when (not (subsetp (list (current-buffer)) (tramp-list-remote-buffers)))
      (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 "lintrunner.py" (list local-file)))))
  (add-to-list 'flymake-allowed-file-name-masks
               '("\\.py\\'" flymake-pyflakes-init))
  (add-hook 'python-mode-hook (lambda () (flymake-mode t)))

  )

動作確認

hoge.pyを開くとこのエントリー始めの画像のような感じになる。
pyflakesのチェックは赤色、PEP8のチェックは青紫色。

メモ

EmacsでのPython環境構築をまとめたほうが良いかな?





*1:追記:20120118