Emacs:如何将flycheck设置为Python 3?

19
我已经在Ubuntu 15.04上为Emacs Python安装了flycheck。
然而,在使用该工具时,它会报告假阳性,例如print(item, end=' ')由于end是错误的语法。
我知道这是Python 3的语法,并且语法错误是由于flycheck设置为Python 2造成的。 如何为Python 3设置flycheck? 在Github上的文档中,它没有提到是否支持Python 2或3(只有Python)。
此外,如果可能的话,请给我一个提示,为什么elpa工具没有为基本的Python类型提供建议。
我的init.el文件在这里:
;; init.el --- Emacs configuration

;; INSTALL PACKAGES
;; -------------------------------------

(require 'package)

;; Primary Emacs repository is MELPA
(add-to-list 'package-archives
         '("melpa" . "http://melpa.org/packages/") t)

(package-initialize)
(when (not package-archive-contents)
  (package-refresh-contents))

(defvar myPackages
  '(better-defaults
    elpy ;; Emacs Lisp Python Environment
    flycheck ;; flycheck Python syntax-checking
    material-theme))

(mapc #'(lambda (package)
      (unless (package-installed-p package)
        (package-install package)))
      myPackages)

;; BASIC CUSTOMIZATION
;; -------------------------------------

(setq inhibit-startup-message t) ;; hide startup message
(load-theme 'material t) ;; load material theme
(global-linum-mode t) ;; enable line numbers globally
(global-flycheck-mode) ;; enable flycheck globally

;; PYTHON CONFIGURATION
;; -------------------------------------
(elpy-enable) ;; enable elpy

;; use flycheck, not flymake with elpy
(when (require 'flycheck nil t)
  (setq elpy-modules (delq 'elpy-module-flymake elpy-modules))
  (add-hook 'elpy-mode-hook 'flycheck-mode))

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(python-shell-interpreter "python3"))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )

;; init.el ends here

我遇到了与 awaitasync 关键字以及 print 函数相同的问题。 - user9903
4个回答

20

Flycheck为每个支持的语言(以及由社区提供的许多语言)都有许多检查器。几乎所有的检查器都使用外部工具来检查缓冲区,大多数可以进行配置。

查找支持哪些检查器以及如何配置它们的最简单方法是通过内省。打开一个Python文件并调用flycheck-verify-setup或按下C-c ! v。这将显示一个新的缓冲区,其中列出了在python-mode中使用的语法检查器。每个检查器都是指向文档的超链接,其中描述了它以及给出了配置选项。

由于我没有python-flake8python-pylint,所以我只需将变量flycheck-python-pycompile-executable设置为"python3"即可。


17

对于我的情况(没有使用 pipenv),我通过将检查器安装到 python3 中使它正常工作:

$ pip3 install flake8 pylint mypy
将以下内容添加到我的~/.emacs.c/custom.el文件中,以便Flycheck在检查时使用python3
(custom-set-variables
 '(flycheck-python-flake8-executable "python3")
 '(flycheck-python-pycompile-executable "python3")
 '(flycheck-python-pylint-executable "python3"))

正如 @uwe-koloska 指出的那样,使用 Ctrl-c ! v 调用 flycheck-verify-setup 很有帮助!


很好,我使用了你的片段来让flake和pylint使用当前项目的虚拟环境(venv)。我在你的片段中将"python3"替换为".venv/bin/python3"。目前这个方法是有效的(显然,这要求你始终将虚拟环境存储在当前项目的".venv"目录中)。 - undefined

9
Flycheck会调用pylint执行程序,该程序应该在您的路径中某个位置。如果该可执行文件是由pip为python2安装的,那么pylint将检查python2语法;如果是使用 pip(有时称为pip3)为python3安装的,则pylint将检查python3语法。
如何进行取决于许多因素。如果您正在使用虚拟环境,则可以从flycheck的创建者点文件 此页面 开始。
此行也是必需的:(add-hook 'flycheck-mode-hook #'flycheck-virtualenv-setup) 如果您没有使用虚拟环境,则只需确保为python3安装了pylint可执行文件即可。

Python 2 的语法是否也是 Python 3 的有效语法? - Shuzheng
大部分情况下,确实存在一些差异。例如,Python3没有execfile和一些其他功能,比如reduce - Jules

2

正如@Jules所提到的:

Flycheck只是调用应该在您的路径中某个地方的pylint可执行文件。

因此,正确的flake8版本在emacs中可访问变得至关重要。由于您正在使用pipenv,所以您应该确保首先在项目环境中安装flake8,然后确保在启动emacs之前激活您的环境。

从您的项目根目录中:

pipenv install flake8
pipenv shell

您可以从命令行启动emacs,然后flycheck将连接适用于您的项目的正确版本的flake8。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接