如何使用带有 pyflakes 和 pylint 检查代码的 Python 的 Emacs Flymake 模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1259873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:48:01  来源:igfitidea点击:

How can I use Emacs Flymake mode for python with pyflakes and pylint checking code?

pythonemacspylintpep8pyflakes

提问by dixon

For checking code in python mode I use flymake with pyflakes

为了在 python 模式下检查代码,我将 flymake 与pyflakes一起使用

Also I want check code style (pep8) with pylint (description on the same page with pyflakes)

我还想用 pylint 检查代码样式(pep8)(与 pyflakes 在同一页面上的描述)

This solutions work. But I can't configure flymake for work with pyflakes and pylint together. How can I do it?

此解决方案有效。但是我无法将 flymake 配置为与 pyflakes 和 pylint 一起使用。我该怎么做?

回答by vaab

Well, flymake is just looking for a executable command thats output lines in a predefined format. You can make a shell script for example that will call successively all the checkers you want...

好吧,flymake 只是在寻找一个可执行命令,它是预定义格式的输出行。例如,您可以制作一个 shell 脚本,该脚本将连续调用您想要的所有检查器...

You must also make sure that your script ends by returning errorlevel 0. So this is an example:

您还必须确保您的脚本以返回错误级别 0 结束。所以这是一个示例:

This is what I've done in a "pycheckers" script:

这是我在“pycheckers”脚本中所做的:

#!/bin/bash

epylint "" 2>/dev/null
pyflakes ""
pep8 --ignore=E221,E701,E202 --repeat ""
true

For the emacs lisp part:

对于 emacs lisp 部分:

(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 "pycheckers"  (list local-file))))
  (add-to-list 'flymake-allowed-file-name-masks
               '("\.py\'" flymake-pyflakes-init)))

回答by RichieHH

Usually one can enable flymake mode in the python-mode-hook. Unfortunately that causes issues with things like py-execute-buffer which create temporary buffers which invoke the hook and then cause flymake mode to hiccup because of the lack of "real file". The solution is to modify the conditions where you add the hook:- e.g mine is:

通常可以在 python-mode-hook 中启用 flymake 模式。不幸的是,这会导致诸如 py-execute-buffer 之类的问题,这些问题会创建临时缓冲区,这些缓冲区会调用钩子,然后由于缺少“真实文件”而导致 flymake 模式打嗝。解决方案是修改添加钩子的条件:- 例如我的是:

(add-hook 'python-mode-hook 
      (lambda () 
        (unless (eq buffer-file-name nil) (flymake-mode 1)) ;dont invoke flymake on temporary buffers for the interpreter
        (local-set-key [f2] 'flymake-goto-prev-error)
        (local-set-key [f3] 'flymake-goto-next-error)
        ))

回答by Bleeding Fingers

Windows batch version of vaab's pychechker

vaab 的 Windows 批处理版本 pychechker

@echo on
pylint %1
pep8 --ignore=E221,E701,E202 --repeat %1
pyflakes %1

回答by Vince

You may want to check out the Lisp script here (http://charlie137-2.blogspot.com/2009/08/check-python-coding-style-on-fly-with.html), which should help with checking PEP8 a la pep8.py. I don't use pyflakes or pylint, but I imagine you could easily adjust this to work with other checkers.

您可能想在这里查看 Lisp 脚本(http://charlie137-2.blogspot.com/2009/08/check-python-coding-style-on-fly-with.html),它应该有助于检查 PEP8一个 la pep8.py。我不使用 pyflakes 或 pylint,但我想您可以轻松调整它以与其他跳棋一起使用。