VIM:在 python 模式下使用 python3 解释器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30444890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:26:00  来源:igfitidea点击:

VIM: Use python3 interpreter in python-mode

pythonvimubuntu-14.04python-3.4python-mode

提问by Cord Kaldemeyer

I have recently switched to vim and configured it for Python-programming using thistutorial. Before, I have made sure that vim supports python3 (vim --version shows +python/dyn and +python3/dyn) using thisarticle.

我最近切换到 vim 并使用教程将其配置为 Python 编程。之前,我使用这篇文章确保 vim 支持 python3(vim --version 显示 +python/dyn 和 +python3/dyn)。

But when executing a file from python-mode, still the python2.7 interpreter is chosen.

但是当从python-mode执行文件时,仍然选择 python2.7 解释器。

How can I configure vim (or the python-mode) to run files on the python3 interpreter?

如何配置 vim(或 python 模式)以在 python3 解释器上运行文件?

My OS is Ubuntu 14.04 x64.

我的操作系统是 Ubuntu 14.04 x64。

Thanks in advance!

提前致谢!

采纳答案by Ray Perea

Try adding this to your .vimrc file

尝试将其添加到您的 .vimrc 文件中

let g:pymode_python = 'python3'

I found this in the help docs. In vim type:

我在帮助文档中找到了这个。在 vim 类型中:

:help python-mode

By default, vim is not compiled with python3 support, so when I tried this, I got all kinds of errors... Which tells me it's trying to use python3. But if your vim --versionoutput shows +python3 you should be good.

默认情况下,vim 没有使用 python3 支持编译,所以当我尝试这个时,我得到了各种各样的错误......这告诉我它正在尝试使用 python3。但是如果你的vim --version输出显示 +python3 你应该很好。

EDIT: By default, Ubuntu 14.04 doesn't come with +python3 support. And due to limitations, you can't have both python2 and python3 support.

编辑:默认情况下,Ubuntu 14.04 不支持 +python3。并且由于限制,您不能同时拥有 python2 和 python3 支持。

So, you have to compile vim with python3 support.

所以,你必须用 python3 支持编译 vim。

These are the steps that worked for me: From a linux command line:

这些是对我有用的步骤: 从 linux 命令行:

Install packages

安装包

sudo apt-get install checkinstall mercurial python-dev python3-dev ruby ruby-dev libx11-dev libxt-dev libgtk2.0-dev libncurses5 ncurses-dev

Grab the latest version of vim

获取最新版本的 vim

hg clone https://vim.googlecode.com/hg/ vim

Configure it

配置它

cd vim
./configure \
--enable-perlinterp \
--enable-python3interp \
--enable-rubyinterp \
--enable-cscope \
--enable-gui=auto \
--enable-gtk2-check \
--enable-gnome-check \
--with-features=huge \
--enable-multibyte \
--with-x \
--with-compiledby="xorpd" \
--with-python3-config-dir=/usr/lib/python3.4/config-3.4m-x86_64-linux-gnu \
--prefix=/opt/vim74

Compile it

编译它

make

Test it

测试一下

make test

Install it

安装它

sudo checkinstall

Link the package

链接包

sudo ln -s /opt/vim74/bin/vim /usr/bin/vim-py3

Now, you have both versions of vim

现在,您拥有两个版本的 vim

To use normal vim (python2) type vim file.py

使用普通 vim (python2) 类型 vim file.py

To use vim with python3 support type vim-py3 file.py

使用带有 python3 支持类型的 vim vim-py3 file.py

If you just want the python3 version, then you only need to link it to the new vim

如果你只想要python3版本,那么你只需要把它链接到新的vim

ln -s /opt/vim74/bin/vim /usr/local/bin/vim

And if you want to switch back to the python2 version, remove the link

如果你想切换回python2版本,删除链接

rm /usr/local/bin/vim

回答by guy

it removes python 2.X

它删除了python 2.X

The symbolic link (/usr/bin/vim-> /etc/alternatives/vim) is become useless, probably because the vim executable deleted from /etc/alternatives

符号链接(/usr/bin/vim-> /etc/alternatives/vim)变得无用,可能是因为 vim 可执行文件从/etc/alternatives

回答by Stryker

I removed the The symbolic link (/usr/bin/vim) since it does not work any more and relinked the vim

我删除了符号链接 (/usr/bin/vim) 因为它不再起作用并重新链接了 vim

ln -s /opt/vim74/bin/vim /usr/bin/vim

回答by u7140332

I also met the same problem. My device is a Mac, so it may be a bit different. I use Homebrew to manage my packages.

我也遇到了同样的问题。我的设备是 Mac,所以可能有点不同。我使用 Homebrew 来管理我的包。

brew install vimwill download Vim with Python.

brew install vim将使用 Python 下载 Vim。

So you can download Vim with Python 3 with brew install vim --with-python3 --HEAD

所以你可以用 Python 3 下载 Vim brew install vim --with-python3 --HEAD

There may be some other operations. You may need something like brew unlink vim.

可能还有其他一些操作。您可能需要类似brew unlink vim.