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

Python027

如何在VIM中指定Python解释器版本,第1张

你可能不想(或至少不应该) code>python3 作为vim的默认python解释器,因为你的插件的一些(大部分)将变得不兼容,例如 YouCompleteMe 和 clang_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 和 -lpython3.x 我建议你从源代码编译vim,具体链接到 -lpython2.x

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

clone vim mercurial

hg clone htvim/

cd vim

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

./configure --with-features = huge \

--enable-cscope \

--enable-pythoninterp \

--enable-largefile \

--with-python-config-dir = / usr / lib / python2.7 / config

您还可能想要链接 ruby 和 lua 如果需要,然后最后运行

make build

make install

这里是shell脚本,将自动执行整个过程为你。这可能有点过分,但我认为这是你应该如何处理这不与运行与您的未来包的兼容性问题。

如未安装vim,使用下面命令安装:

sudo apt-get install vim

在用户目录下创建.vim目录,其下创建bundle目录:

mkdir .vim\bundle

安装Bundle/Vundle:

git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle

创建或编辑用户目录下的.vimrc文件,添加如下内容:

set nocompatible

set rtp+=~/.vim/bundle/vundle/

call vundle#rc()

" let Vundle manage Vundle

" required!

Bundle 'gmarik/vundle'

" The bundles you install will be listed here

Bundle 'scrooloose/nerdtree'

Bundle 'davidhalter/jedi-vim'

filetype plugin indent on

" The rest of your config follows here.

augroup vimrc_autocmds

autocmd!

" highlight characters past column 120

autocmd FileType python highlight Excess ctermbg=DarkGrey guibg=Black

autocmd FileType python match Excess /\%120v.*/

autocmd FileType python set nowrap

augroup END

" NerdTree Shortcut.

map <f2>:NERDTreeToggle<cr>

" automatically change window’s cwd to file’s dir

set autochdir

" Prefer spaces to tabs

set tabstop=4

set shiftwidth=4

set expandtab

set nu!

打开vim,键入命令“:BundleInstall”,等待自动安装插件完成(下面显示“done”)

在vim中键入命令“:BundleList”可以查看安装的插件。

问题可能来自两方面: vim 没有把 python 支持特性编译进来;或者编译支持的 python 与本地安装的版本不兼容。

1. 只有编译的时候带了 python 支持的 vim 才能使用相关的功能。要想知道 vim 是否带了编译时的支持,使用:

:version

看有没有 +python 之类的字样。如果是 + 号,表示支持,如果是 - 号就是不支持(像我截图中标出的 postscript 就是 -)。python 代表 python 2,python3 表示 python 3 的支持情况。

2. 如果是 + 号,那么要看编译时支持的 python 是不和你电脑上现在已经安装的 python 相容。用:

set pythondll?

set pythonthreedll?

分别来看编译时支持的 python2 或 python3 版本。结果可能是:

pythonthreedll=python35.dll

这样你的电脑里必需要安装 python 3.5 才可以配合 vim 使用。注意一定要是标准版的才行,定制版(比如 Anaconda)是不行的。

vim 和 python 有相当深入的交互。除了简单的交互执行 python 语句和返回结果外,vim 的内部的数据结构(比如缓冲区、窗口之类的)和 vim-script 脚本的执行接口也全对 python 开放。因此对 python 各版本支持的灵活性就要差一些,需要在编译阶段就确定。

如果想兼容你电脑上已有的 python,需要在你的电脑上重新编译 vim,这通常比较困难。在你的电脑上配置两套 python 环境要相对容易很多。或者如果你对 python 的要求并不高的话,就用 vim 支持的那版也行。

以上的回答假定您已经对 python 有一定的了解,并且可以自己正确的安装和配置 python 的环境。如果抛开 vim,python 都不能正确执行,那么需要先解决 python 的安装和配置问题。