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

2024-06-07 15:37:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Ubuntu15.04上安装了EmacsPython的flycheck。

但是,当使用该工具时,它会报告诸如print(item, end=' ')之类的误报,这是由于end导致的语法错误。

我知道这是Python3语法,而语法错误是由为Python2设置的flycheck引起的。

如何为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

Tags: youpackagebyifinitenablemodecustom
3条回答

Flycheck只需调用pylint可执行文件,该文件应该位于您路径中的某个位置。如果可执行文件是由pip为python2安装的,那么pylint将检查python2语法如果是为pip(有时称为pip3)为python3安装的,那么pylint将检查python3语法。

如何进行取决于许多事情。

如果您使用的是虚拟环境,那么从flycheck的创建者的dotfiles开始on this page

这一行也是必需的:(add-hook 'flycheck-mode-hook #'flycheck-virtualenv-setup)

如果您没有使用虚拟环境,那么您可以确保pylint可执行文件是为python3安装的

Flycheck为每个supported language(以及更多的provided by the community)都有许多跳棋。它们几乎都使用外部工具来检查缓冲区,而且大多数都可以配置。

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

因为我既没有python-flake8,也没有python-pylint,所以我只将变量flycheck-python-pycompile-executable设置为"python3",一切都是光明和愉快的。

对于我的例子(没有pipenv),我是通过将checkers安装到python3中来实现的:

$ pip3 install flake8 pylint mypy

并将以下内容添加到my~/.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-cv呼叫flycheck-verify-setup非常有用!

相关问题 更多 >

    热门问题