Emacs: How do I set flycheck to Python 3?

2020-06-08 03:41发布

I've installed flycheck for Emacs Python on Ubuntu 15.04.

However, when using the tool it reports false positives like print(item, end=' ') is wrong syntax due to end.

I know this is Python 3 syntax, and that the syntax error is caused by flycheck is set up for Python 2.

How do I set flycheck up for Python 3?

In the documentation on Github, it doesn't mention whether it supports Python 2 or 3 (just Python).

Also, if possible, give me a hint why the elpa tool is not giving suggestions for e.g. basic Python types.

My init.el file is here:

;; 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

标签: python emacs
4条回答
家丑人穷心不美
2楼-- · 2020-06-08 04:06

For my case (no pipenv), I got it working by installing the checkers into python3:

$ pip3 install flake8 pylint mypy

And adding the following to my ~/.emacs.c/custom.el so Flycheck uses python3 for the checkers:

(custom-set-variables
 '(flycheck-python-flake8-executable "python3")
 '(flycheck-python-pycompile-executable "python3")
 '(flycheck-python-pylint-executable "python3"))

As @uwe-koloska noted, using Ctrl-c ! v to call flycheck-verify-setup is very helpful!

查看更多
The star\"
3楼-- · 2020-06-08 04:06

Flycheck simply calls the pylint executable that should be somewhere in your path. If that executable was installed by pip for python2 then pylint will check python2 syntax if it was installed for pip (sometimes called pip3) for python3 then pylint will check python3 syntax.

How to proceed depends on a number of things.

If you are using virtual environments then a good place to start is on this page from flycheck's creator's dotfiles

This line is also necessary: (add-hook 'flycheck-mode-hook #'flycheck-virtualenv-setup)

If you are not using virtual environments then you can just make sure that the pylint executable was installed for python3

查看更多
闹够了就滚
4楼-- · 2020-06-08 04:10

As @Jules mentioned:

Flycheck simply calls the pylint executable that should be somewhere in your path.

So it becomes critical that the correct flake8 version is accessible within emacs. Since you are using pipenv, you should be sure to first install flake8 within your project's environment, and then ensure that your environment is activated before launching emacs.

From within your project root directory:

pipenv install flake8
pipenv shell

You can then launch emacs from the commandline, and flycheck will connect with the correct version of flake8 for your project.

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2020-06-08 04:16

Flycheck has a number of checkers for each supported language (and many more provided by the community). Nearly all of them use external tools to check buffers and most of them can be configured.

The easiest way to find out what checkers are supported and how to configure them is by introspection. Open a python file and call flycheck-verify-setup or press C-c ! v. This shows you a new buffer with a list of syntax checkers used in python-mode. Every checker is a hyperlink to the documentation where it is described and the configuration options are given.

Since I don't have python-flake8 nor python-pylint I just set the variable flycheck-python-pycompile-executable to "python3" and all is bright and cheerful.

查看更多
登录 后发表回答