如何在VIM中指定Python解释器的版本?

9
为了在Vim中使用插件Clang_complete编写C++代码。 安装后,出现以下错误:
Error detected while processing function <SNR>14_ClangCompleteInit..<SNR>14_initClangCompletePython:
clang_complete: No python support available.
Cannot use clang library
Compile vim with python support to use libclang

但是我的vim同时支持Python和Python3。+python/dyn +python3/dyn
然后我检查了我的Python解释器:

:echo has('python')      # output is 0
:echo has('python3')     # output is 1

我认为原因是我的vim默认动态加载python3解释器。
我可以更改Python解释器吗?或者设置默认动态加载的Python版本?

2个回答

4
您可能不希望(或至少不应该)将python3设置为vim的默认python解释器,因为这样一来,您的某些(大多数)插件将变得不兼容,例如YouCompleteMeclang_complete本身,因为它们不支持python3。通常支持python3的插件会让您决定是否使用它,方法是将其添加到您的.vimrc中。
let g:syntastic_python_python_exec = 'python3' 

解决方案::echo has('python') 显示 0 实际上是告诉您 vim 可能没有使用 python2 编译。因此,请首先检查 vim --version 的输出,您应该能够看到编译器构建 vim 时使用的共享库列表。您是否看到了以下内容?(例如 Python 2.7):

-L/usr/lib/python2.7/config-x86_64-linux-gnu -lpython2.7

如果您没有在编译时指定 -lpython2.x 或者如果您同时看到了 -lpython2.x-lpython3.x,我建议您从源代码编译 Vim,并将其链接到 -lpython2.x。从源代码构建 Vim 并不难。首先,请确保删除所有当前安装的 Vim 版本,例如使用 aptitude 命令:

sudo apt-get remove --purge vim vim-runtime vim-gnome vim-tiny vim-common vim-gui-common

克隆 Vim Mercurial

hg clone https://code.google.com/p/vim/
cd vim

然后使用以下标志运行./configure

 ./configure --with-features=huge \
        --enable-cscope \
        --enable-pythoninterp \
        --enable-largefile \
        --with-python-config-dir=/usr/lib/python2.7/config 

如果需要的话,您还可以链接到 rubylua,最后运行以下命令:

make build
make install

这里有一个shell脚本可以自动化整个过程,链接如下:https://github.com/romeric/scripts/blob/master/install_vim.sh。这可能有点过度,但我认为这是应该处理的方式,以避免与未来软件包的兼容性问题。

谢谢您的回答!我重新编译了我的vim,问题已经解决了。 但是我还有另一个问题,+python/dyn +python3/dyn是什么意思? - Kai7
这难道不意味着我可以在vim中使用py或py3命令吗?(备注:我检查了我的原始vim版本,实际上它没有“-L/usr/lib/python2.7/config-x86_64-linux-gnu -lpython2.7”) - Kai7
1
这意味着vim动态链接到了python2python3,但并没有特别编译使用python标志或默认选择python3。如果你使用sudo apt-get install vim安装vim,就会出现这种情况。我可能错了,但是以这种方式安装vim可能实际上不会显示编译器标志。 - romeric

0
" .gvimrc

let &pythonthreedll='C:\...\python37.dll'
let &pythonthreehome='C:\...\Python37'

检查:

:py3 print('hello')

并且VIM应该与Python匹配(我指的是64位、32位)

Windows Vim x64下载


只有在使用已编译具备 "+python3/dyn" 功能的 vim 版本时,此方法才有效。 - Fabio Almeida

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接