52

Is it possible to change the python version used by syntastic for syntax checking?

As the Issue https://github.com/scrooloose/syntastic/issues/385 indicates I could use virtual-env. But is it also possible just with syntastic or vim commands?

mjb4
  • 989
  • 1
  • 9
  • 14

8 Answers8

48

Easiest solution:

Add this to you .vimrc

 let g:syntastic_python_python_exec = 'python3'
 let g:syntastic_python_checkers = ['python']

This is the straightforward solution to switch to python3.

Raphael D.
  • 778
  • 2
  • 7
  • 18
20

The below is no longer necessary, and might screw up if you're forced to work on a strictly python 2.x script.

The best option is to leave the Syntastic defaults alone, and to use conda to manage separate environments for python 3 and 2 (each with their own version-specific installs of flake8, pyflakes, etc), and to switch to the appropriate environment to edit each file. Syntastic will then call python/flake8/whatever else according to the paths set in the activated environment.


From the Syntastic repository README:

Q. The python checker complains about syntactically valid Python 3 constructs...

A. Configure the python checker to call a Python 3 interpreter rather than Python 2, e.g:

let g:syntastic_python_python_exec = '/path/to/python3'

Add that line to your .vimrc - that should fix your problem.

naught101
  • 18,687
  • 19
  • 90
  • 138
  • 3
    Doesn't work for me, with syntastic de5e025ef0b8a9eec588d618ebaebd104945af4c. I'm getting syntax errors from valid Python 3 constructs. `:!python3 -m flake8 %` works fine. – Marius Gedminas Mar 22 '15 at 14:08
  • 5
    @Marius Gedminas That fine, since `g:syntastic_python_python_exec` refers to the `python` checker, which is unrelated to the `flake8` checker. If you want to run `flake8` under Python 3 either install a flake8 compiled against Python 3, or write a wrapper script, or set `g:syntastic_python_flake8_exe` (not `*_exec`, see the manual for the difference) to `'python3 -m flake8'`. – lcd047 Mar 22 '15 at 17:14
  • 3
    I'm now using these settings, which uses either the system `python` or the `python` defined by whatever virtualenv I'm in - I use a virtualenv for my python3 work: `let g:syntastic_python_checkers=['pylint']` `let g:syntastic_python_python_exec = 'python'` `let g:syntastic_python_pylint_exe = 'python -m pylint'` Obviously, I use `pyflakes` rather than `flake8`... – egeland May 03 '15 at 12:41
  • For me it makes Syntastic completely blind to any BS I hand to it. – Błażej Michalik Aug 29 '16 at 13:49
  • Also, I think that this solution interferes with virtualenvs. – Błażej Michalik Aug 29 '16 at 14:22
  • @BłażejMichalik: well, are you handing it the path to the virtualenv version of python? BTW, ditch virtualenv, and move to conda. – naught101 Aug 29 '16 at 23:25
  • 2
    Please note that this is no longer the recommended approach these days. Just install `virtualenv` or `pyenv`, install the relevant checkers inside, and run Vim from the virtual environment (cf. [FAQ](https://github.com/vim-syntastic/syntastic#faqpython)). This has the advantage that it works for _all_ Python checkers, not just for `python`. – lcd047 Feb 01 '17 at 16:02
18

In spite of all the answers here, I still find the recommendation from the FAQ to be the best. I have added this to my .vimrc so that I can easily switch between python versions.

function Py2()
  let g:syntastic_python_python_exec = '/usr/local/bin/python2.7'
endfunction

function Py3()
  let g:syntastic_python_python_exec = '/usr/local/bin/python3.6'
endfunction

call Py3()   " default to Py3 because I try to use it when possible

With those functions installed, it's easy to switch python version right within vim with :call Py2() or :call Py3() depending on what I need at the moment. No need to exit vim and activate a different virtualenv as the popular answer would have you do.

Bruno Bronosky
  • 66,273
  • 12
  • 162
  • 149
8

I managed to convince Syntastic to handle Python 3 syntax with

pip3 install --user flake8

(to make python3 -m flake8 *.py work) and then, in vim:

let g:syntastic_python_flake8_exec = 'python3'
let g:syntastic_python_flake8_args = ['-m', 'flake8']
Marius Gedminas
  • 11,010
  • 4
  • 41
  • 39
  • 3
    ... Except (1) this doesn't affect the `python` checker, and (2) it makes syntastic believe flake8 is installed even when it isn't. Set `g:syntastic_python_flake8_exe` (not `exec`) to `'python3 -m flake8'` if you insist to do it this way. – lcd047 Mar 22 '15 at 17:18
  • Thank you; apparently I forgot that flake8 wasn't the default Python checker in Syntastic! – Marius Gedminas Mar 23 '15 at 12:21
  • You probably meant `let g:syntastic_python_flake8_exec = 'python3'`. Right? – Tarrasch Nov 24 '15 at 04:21
  • 1
    Oops, yes, thank you! Although today I use `command! -bar Python2 let g:syntastic_python_flake8_exe = 'python2 -m flake8' | SyntasticCheck` and `command! -bar Python3 let g:syntastic_python_flake8_exe = 'python3 -m flake8' | SyntasticCheck` – Marius Gedminas Nov 24 '15 at 12:06
  • I'm using pyenv and this answer worked perfectly for me. Thanks! – James Robert Albert Aug 30 '17 at 17:09
3

Not really, but you can get the python3 incompatible warning by install this package.

Let's say your current Syntastic Python checker is python and you want to get python3 incompatible warning. In command mode, you can add py3kwarn to g:syntastic_python_checkers by

:let g:syntastic_python_checkers=['python', 'py3kwarn']

and switch to python2.x only

:let g:syntastic_python_checkers=['python']
attomos
  • 1,112
  • 3
  • 16
  • 30
  • 1
    but this just works for a vim that is using python 2. I have exactly the reverse problem! Still it is a begining! thx – mjb4 Apr 28 '14 at 00:14
  • 1
    Upvoting because I needed to set vim to use python27, instead of the system default 26 and the accepted answer did not work. `let g:syntastic_python_checkers=['python27']`, did. – John C Earls Oct 17 '16 at 17:32
3

If you working under virtualenv, you can use a script that detects current python version and invokes flake8 accordingly. Put the following somewhere in your path and name is flake8.sh:

#!/bin/sh
PYMAJOR=$(python --version | awk '{print $2}'| awk -F. '{print $1}')
exec "/usr/bin/python$PYMAJOR" /usr/bin/flake8 "$@"

Now in you vimrc add:

let g:syntastic_python_flake8_exe='flake8.sh'

Also make sure that both python-flake8 and python3-flake8 (on Ubuntu) are installed.

jjcf89
  • 468
  • 1
  • 5
  • 13
Zaar Hai
  • 9,152
  • 8
  • 37
  • 45
1

Only I did to fix this was to do:

  let g:syntastic_python_flake8_exec = '/path/to/python3'

To make sure flake8 is synced with Python3's syntax. Even when I'm in a virtualenv works.

0

Just to iterate on Zaar Hai's script a bit, something like this should work and be a bit more robust.

#!/usr/bin/env bash

_python=$(command -v python)

[[ "$(uname -s)" =~ Darwin ]] && IS_OSX=true

if [[ "$IS_OSX" ]]; then
    if command -v 'greadlink' >/dev/null 2>&1; then
        greadlink -f "$_python"
    else
        echo 'Install coreutils!' >&2
    fi
else
    readlink -f "$_python"
fi
Community
  • 1
  • 1
jhrr
  • 1,624
  • 14
  • 22