转换Python版本的Vim和Syntastic。

时间:2021-02-22 17:23:59

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

是否可以更改syntastic使用的python版本以进行语法检查?

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?

由于问题https://github.com/scrooloose/syntas/issues /385表明我可以使用virtual-env。但是仅仅使用syntastic或者vim命令也可以吗?

7 个解决方案

#1


18  

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

下面的内容不再需要了,如果您*使用严格的python 2,可能会搞砸。x脚本。

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.

最好的选择是不使用语法缺省值,并使用conda来管理python 3和2的独立环境(每个环境都有自己的flake8、pyflakes等版本特有的安装),并切换到适当的环境来编辑每个文件。然后,Syntastic会根据激活环境中设置的路径调用python/flake8/其他任何东西。


From the Syntastic repository README:

从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:

a .配置python检查器来调用python 3解释器而不是python 2,例如:

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

让旅客:syntastic_python_python_exec = ' / path / / python3 '

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

添加这一行到你的。vimrc -应该可以解决你的问题。

#2


8  

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.

尽管这里有所有的答案,我仍然认为FAQ中的推荐是最好的。我已经将它添加到.vimrc中,以便在python版本之间轻松切换。

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.

安装了这些函数后,很容易在vim内切换python版本:调用Py2()或:调用Py3(),这取决于我现在需要什么。不需要退出vim并激活一个不同的virtualenv,正如流行的答案所要求的那样。

#3


7  

I managed to convince Syntastic to handle Python 3 syntax with

我设法说服Syntastic去处理Python 3语法

pip3 install --user flake8

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

(使python3 -m flake8 *)然后,在vim中:

let g:syntastic_python_flake8_exec = 'python3'
let g:syntastic_python_flake8_args = ['-m', 'flake8']

#4


3  

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

不是真的,但是您可以通过安装这个包来获得python3不兼容的警告。

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

假设您当前的Syntastic Python检查器是Python,您希望得到python3不兼容的警告。在命令模式中,可以将py3kwarn添加到g:syntastic_python_checkers by

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

and switch to python2.x only

和python2开关。x只

:let g:syntastic_python_checkers=['python']

#5


2  

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:

如果您在virtualenv下工作,您可以使用一个脚本检测当前的python版本并相应地调用flake8。在你的路径上写上以下内容,名字就叫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:

现在在你的vimrc中添加:

let g:syntastic_python_flake8_exe='flake8.sh'

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

还要确保在Ubuntu上安装了python-flake8和python3-flake8。

#6


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.

确保flake8与Python3的语法是同步的。即使当我在一个虚拟世界里工作。

#7


0  

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

稍微重复一下Zaar Hai的脚本,类似这样的东西应该是有用的,并且更加健壮。

#!/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

#1


18  

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

下面的内容不再需要了,如果您*使用严格的python 2,可能会搞砸。x脚本。

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.

最好的选择是不使用语法缺省值,并使用conda来管理python 3和2的独立环境(每个环境都有自己的flake8、pyflakes等版本特有的安装),并切换到适当的环境来编辑每个文件。然后,Syntastic会根据激活环境中设置的路径调用python/flake8/其他任何东西。


From the Syntastic repository README:

从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:

a .配置python检查器来调用python 3解释器而不是python 2,例如:

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

让旅客:syntastic_python_python_exec = ' / path / / python3 '

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

添加这一行到你的。vimrc -应该可以解决你的问题。

#2


8  

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.

尽管这里有所有的答案,我仍然认为FAQ中的推荐是最好的。我已经将它添加到.vimrc中,以便在python版本之间轻松切换。

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.

安装了这些函数后,很容易在vim内切换python版本:调用Py2()或:调用Py3(),这取决于我现在需要什么。不需要退出vim并激活一个不同的virtualenv,正如流行的答案所要求的那样。

#3


7  

I managed to convince Syntastic to handle Python 3 syntax with

我设法说服Syntastic去处理Python 3语法

pip3 install --user flake8

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

(使python3 -m flake8 *)然后,在vim中:

let g:syntastic_python_flake8_exec = 'python3'
let g:syntastic_python_flake8_args = ['-m', 'flake8']

#4


3  

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

不是真的,但是您可以通过安装这个包来获得python3不兼容的警告。

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

假设您当前的Syntastic Python检查器是Python,您希望得到python3不兼容的警告。在命令模式中,可以将py3kwarn添加到g:syntastic_python_checkers by

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

and switch to python2.x only

和python2开关。x只

:let g:syntastic_python_checkers=['python']

#5


2  

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:

如果您在virtualenv下工作,您可以使用一个脚本检测当前的python版本并相应地调用flake8。在你的路径上写上以下内容,名字就叫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:

现在在你的vimrc中添加:

let g:syntastic_python_flake8_exe='flake8.sh'

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

还要确保在Ubuntu上安装了python-flake8和python3-flake8。

#6


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.

确保flake8与Python3的语法是同步的。即使当我在一个虚拟世界里工作。

#7


0  

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

稍微重复一下Zaar Hai的脚本,类似这样的东西应该是有用的,并且更加健壮。

#!/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