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

发布于 2024-07-30 06:24:37 字数 262 浏览 11 评论 0 原文

为了在 python 模式下检查代码,我使用 Flymake 和 pyflakes

我也想检查代码风格(pep8)与 pylint (与 pyflakes 位于同一页面上的说明)

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

For checking code in python mode I use flymake with pyflakes

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌︼了一个春 2024-08-06 06:24:38

vaab 的 pychechker 的 Windows 批处理版本

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

Windows batch version of vaab's pychechker

@echo on
pylint %1
pep8 --ignore=E221,E701,E202 --repeat %1
pyflakes %1
相对绾红妆 2024-08-06 06:24:38

通常可以在 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)
        ))

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:

(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)
        ))
旧梦荧光笔 2024-08-06 06:24:38

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

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.

倥絔 2024-08-06 06:24:37

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

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

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

#!/bin/bash

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

对于 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)))

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...

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

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

#!/bin/bash

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

For the emacs lisp part:

(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)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文