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!

Community
  • 1
  • 1
Cord Kaldemeyer
  • 6,405
  • 8
  • 51
  • 81
  • 1
    have you tried [`let g:pymode_python = 'python3'`](https://github.com/klen/python-mode/blob/c6a872a64530979f4aac46d765c577581cb7fc36/doc/pymode.txt#L121) – mata May 25 '15 at 19:46

4 Answers4

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
Ray Perea
  • 5,640
  • 1
  • 35
  • 38
  • 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 (https://www.dropbox.com/s/4u1voj52j2rlfj6/vim_version.txt?dl=0). Here's also the output of :PymodeTroubleshooting (https://www.dropbox.com/s/tiicchty7rgwn69/pymode_troubleshooting.txt?dl=0). Any hints? – Cord Kaldemeyer May 26 '15 at 07:21
  • The error that pops up (E837) es described in the Vim documentation (http://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.. – Cord Kaldemeyer May 26 '15 at 10:32
  • 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. – Ray Perea May 26 '15 at 11:01
  • Btw: I haven't included the `let g:pymode_python = 'python3'` line anymore since I only use Python3! – Cord Kaldemeyer May 26 '15 at 12:40
  • 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' – djangonaut Oct 31 '15 at 12:34
  • 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. – Stryker Jun 22 '16 at 13:48
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

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
guy
  • 11
  • 1
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
Stryker
  • 5,732
  • 1
  • 57
  • 70
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.

Badacadabra
  • 8,043
  • 7
  • 28
  • 49
mc_pengzi
  • 114
  • 1
  • 5