Emacs:如何将flycheck设置为Python 3?
我已经在Ubuntu 15.04上安装了针对Emacs Python的flycheck 但是,当使用该工具时,它会报告误报,如Emacs:如何将flycheck设置为Python 3?,python,emacs,Python,Emacs,我已经在Ubuntu 15.04上安装了针对Emacs Python的flycheck 但是,当使用该工具时,它会报告误报,如print(item,end='')由于end而导致语法错误 我知道这是Python3语法,语法错误是由为Python2设置的flycheck引起的 如何设置Python 3的flycheck? 在Github的文档中,没有提到它是支持Python 2还是3(只是Python) 另外,如果可能的话,请给我一个提示,说明为什么elpa工具没有为基本Python类型提供建议
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
Flycheck只调用应该在路径中某个位置的pylint可执行文件。如果该可执行文件是由pip为python2安装的,那么pylint将检查python2语法如果它是为pip(有时称为pip3)为python3安装的,那么pylint将检查python3语法 如何进行取决于许多事情 如果您使用的是虚拟环境,那么最好从flycheck的创建者的dotfiles开始 这一行也是必需的:
(添加hook'flycheck mode hook#'flycheck-virtualenv-setup)
如果您不使用虚拟环境,那么您可以确保python3安装了pylint可执行文件,Flycheck为每个文件(以及更多文件)安装了多个检查器。几乎所有这些工具都使用外部工具来检查缓冲区,并且大多数都可以配置 找出支持哪些检查器以及如何配置它们的最简单方法是通过内省。打开python文件并调用
flycheck verify setup
或按C-C!v
。这将显示一个新的缓冲区,其中包含python模式中使用的语法检查器列表。每个检查器都是一个指向文档的超链接,其中对其进行了描述,并提供了配置选项
因为我没有python-flake 8
也没有python-pylint
我只是将变量flycheck python-pycompile-executable
设置为“python3”
,一切都是光明而愉快的。正如@Jules所提到的:
Flycheck只调用应该在某处的pylint可执行文件 在你的道路上 因此,在emacs中访问正确的flake8版本变得至关重要。由于您使用的是
pipenv
,因此应确保首先在项目环境中安装flake8
,然后确保在启动emacs之前激活了环境
从项目根目录中:
pipenv install flake8
pipenv shell
然后,您可以从命令行启动emacs,flycheck将为您的项目连接到正确版本的flake8。对于我的案例(无pipenv),我通过将检查器安装到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-c!v调用
flycheck验证设置
非常有用 Python2语法对Python3语法有效吗?不过,在大多数情况下,存在一些差异。例如,python3没有execfile
和其他一些功能,例如reduce,我对wait
和async
关键字以及print
功能也有同样的问题