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

Shu*_*eng 15 python emacs

我在Ubuntu 15.04上为Emacs Python安装了flycheck.

但是,在使用该工具时,它会报告误报,例如print(item, end=' ')错误的语法end.

我知道这是Python 3语法,并且为Python 2设置了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
Run Code Online (Sandbox Code Playgroud)

Uwe*_*ska 14

Flycheck为每种支持的语言提供了许多检查程序(还有更多由社区提供).几乎所有这些都使用外部工具来检查缓冲区,并且可以配置大多数缓冲区.

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

因为我没有,也python-flake8没有python-pylint把变量设置flycheck-python-pycompile-executable为"python3",所有都是明亮和开朗的.


Tom*_*Tom 10

对于我的情况(没有pipenv),我通过将Checkers安装到中来使其工作python3:

$ pip3 install flake8 pylint mypy
Run Code Online (Sandbox Code Playgroud)

并将以下内容添加到我的~/.emacs.c/custom.elFlycheck python3用于检查程序中:

(custom-set-variables
 '(flycheck-python-flake8-executable "python3")
 '(flycheck-python-pycompile-executable "python3")
 '(flycheck-python-pylint-executable "python3"))
Run Code Online (Sandbox Code Playgroud)

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


Jul*_*les 7

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

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

如果您正在使用虚拟环境,那么从flycheck的创建者的dotfiles 开始,这个页面就是一个很好的起点

这条线也是必要的: (add-hook 'flycheck-mode-hook #'flycheck-virtualenv-setup)

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