Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: How do I set flycheck to Python 3?

Tags:

python

emacs

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
like image 718
Shuzheng Avatar asked Jun 09 '16 08:06

Shuzheng


People also ask

What is flycheck emacs?

Flycheck is a modern on-the-fly syntax checking extension for GNU Emacs, intended as replacement for the older Flymake extension which is part of GNU Emacs. For a detailed comparison to Flymake see Flycheck versus Flymake.

How do I install Flycheck?

use-packageSpecifically it allows to automatically install missing packages from package archive when Emacs starts. Then press C-M-x with point somewhere in this form to install and enable Flycheck for the current Emacs session.


1 Answers

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.

like image 94
Uwe Koloska Avatar answered Sep 27 '22 19:09

Uwe Koloska