Search code examples
pythonvimubuntu-14.04python-3.4python-mode

VIM: Use python3 interpreter in python-mode


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!


Solution

  • 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