Reputation: 987
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?
Upvotes: 52
Views: 21655
Reputation: 778
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.
Upvotes: 48
Reputation: 9879
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.
Upvotes: 3
Reputation: 70359
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.
Upvotes: 18
Reputation: 19553
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.
Upvotes: 20
Reputation: 125
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.
Upvotes: 1
Reputation: 1730
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
Upvotes: 0
Reputation: 11337
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']
Upvotes: 8
Reputation: 1202
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']
Upvotes: 3