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
For my case (no pipenv), I got it working by installing the checkers into
python3
:And adding the following to my
~/.emacs.c/custom.el
so Flycheck usespython3
for the checkers:As @uwe-koloska noted, using Ctrl-c ! v to call
flycheck-verify-setup
is very helpful!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
As @Jules mentioned:
So it becomes critical that the correct flake8 version is accessible within emacs. Since you are using
pipenv
, you should be sure to first installflake8
within your project's environment, and then ensure that your environment is activated before launching emacs.From within your project root directory:
You can then launch emacs from the commandline, and flycheck will connect with the correct version of flake8 for your project.
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 pressC-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
norpython-pylint
I just set the variableflycheck-python-pycompile-executable
to"python3"
and all is bright and cheerful.