簡體   English   中英

Emacs:如何將flycheck設置為Python 3?

[英]Emacs: How do I set flycheck to Python 3?

我在Ubuntu 15.04上為Emacs Python安裝了flycheck。

但是,當使用該工具時print(item, end=' ')由於end print(item, end=' ')它報告像print(item, 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

Flycheck為每種支持的語言 提供了許多檢查程序(還有更多由社區提供 )。 幾乎所有這些都使用外部工具來檢查緩沖區,並且可以配置大多數緩沖區。

找出支持哪些檢查器以及如何配置它們的最簡單方法是內省。 打開python文件並調用flycheck-verify-setup或按Cc ! v Cc ! v 這將顯示一個新緩沖區,其中包含python-mode中使用的語法檢查器列表。 每個檢查器都是指向描述它的文檔的超鏈接,並給出了配置選項。

由於我沒有python-flake8python-pylint我只是將變量flycheck-python-pycompile-executable"python3" ,所有這些都是明亮而愉快的。

對於我的情況(沒有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非常有幫助!

Flycheck只是調用應該在路徑中的某個位置的pylint可執行文件。 如果那個可執行文件是由python2的pip安裝的那么pylint將檢查python2語法,如果它是為python3安裝pip(有時稱為pip3)那么pylint將檢查python3語法。

如何進行取決於許多事情。

如果您正在使用虛擬環境,那么從flycheck的創建者的dotfiles開始,這個頁面就是一個很好的起點

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

如果您沒有使用虛擬環境,那么您可以確保為python3安裝了pylint可執行文件

正如@Jules所說:

Flycheck只是調用應該在路徑中的某個位置的pylint可執行文件。

因此,在emacs中可以訪問正確的flake8版本變得至關重要。 由於您使用的是pipenv ,因此您應該確保首先在項目環境中安裝flake8 ,然后確保在啟動emacs之前激活您的環境。

從項目根目錄中:

pipenv install flake8
pipenv shell

然后,您可以從命令行啟動emacs,flycheck將為您的項目連接正確版本的flake8。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM