VIM: Use python3 interpreter in python-mode
Asked Answered
T

4

12

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

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

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

My OS is Ubuntu 14.04 x64.

Thanks in advance!

Tardy answered 25/5, 2015 at 19:31 Comment(1)
have you tried let g:pymode_python = 'python3'Sienkiewicz
G
21

Try adding this to your .vimrc file

let g:pymode_python = 'python3'

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

: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 --version output shows +python3 you should be good.

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.

So, you have to compile vim with python3 support.

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

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

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

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

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

If you just want the python3 version, then you only need to link it to the new 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

rm /usr/local/bin/vim
Gadabout answered 26/5, 2015 at 3:48 Comment(6)
Thanks for both of your comments! I have tried this and now get an Error E837: Dieses Vim kann :py3 nicht nach der Verwendung von :python ausführen E263: Dieser Befehl ist nicht verfügbar, die Python-Bibliothek konnte nicht geladen werden, saying that :py3 cannot be executed after using :python. My vim does support python3. At least it says so in the output of vim --version (dropbox.com/s/4u1voj52j2rlfj6/vim_version.txt?dl=0). Here's also the output of :PymodeTroubleshooting (dropbox.com/s/tiicchty7rgwn69/pymode_troubleshooting.txt?dl=0). Any hints?Tardy
The error that pops up (E837) es described in the Vim documentation (vimdoc.sourceforge.net/htmldoc/if_pyth.html#python-dynamic) with some workarounds. But I dont't know how to rebuild it only for one python version..Tardy
That seems to be the real problem is that Ubuntu 14.04 doesn't come with vim that's compiled to run python3. If I get some time tomorrow, I will try to re-compile vim to see if I can get it to work... I'll let you know what I find.Gadabout
Btw: I haven't included the let g:pymode_python = 'python3' line anymore since I only use Python3!Tardy
Thanks a lot! I had to execute 'C_INCLUDE_PATH=/usr/include/python3.4m/ make' as it complained about not finding Python.h despite the python3.4-dev package being already installed. I found the location via 'dpkg-query -L libpython3.4-dev | grep Python.h'Passe
Thanks a lot! ...worked like a charm. These steps removed the original vim but not I have vim-py3 which includes support for python3. great steps. thanks.Humus
I
1

it removes python 2.X

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

Immovable answered 5/1, 2016 at 13:31 Comment(0)
H
0

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

ln -s /opt/vim74/bin/vim /usr/bin/vim
Humus answered 22/6, 2016 at 13:52 Comment(0)
C
0

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.

brew install vim will download Vim with Python.

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

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

Clubwoman answered 24/4, 2017 at 13:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.