2

I have vim installed and it is configured with +python3/dyn. If I run vim --noplugin I can use py3. However with my plugins enabled, I get the following error:

E837: This Vim cannot execute :py3 after using :python
E263: Sorry, this command is disabled, the Python library could not be loaded.

I suspect that one of the plugins loads python2 and therefore defines which python version is being used (similar to this vim-bootstrap issue). The problem is I don't know which.

How can I use python3 with my vim version?


Plungins Installed

The following plugins are installed with Vundle:

 60 " let Vundle manage Vundle, required
 61 Plugin 'gmarik/Vundle.vim'
 62 "Bundle 'Valloric/YouCompleteMe'
 63 Plugin 'tmhedberg/SimpylFold'
 64 Plugin 'vim-scripts/indentpython.vim'
 65 Plugin 'scrooloose/syntastic'
 66 Plugin 'scrooloose/nerdtree'
 67 Plugin 'jistr/vim-nerdtree-tabs'
 68 Plugin 'kien/ctrlp.vim'
 69 Plugin 'tpope/vim-fugitive'
 70 Plugin 'nvie/vim-flake8'
 71 "Plugin 'Lokaltog/powerline', {'rtp': 'powerline/bindings/vim/'}
 72 Plugin 'vim-airline/vim-airline'
 73 Plugin 'vim-airline/vim-airline-themes'
 74 Bundle 'klen/python-mode'
 75 Plugin 'jmcantrell/vim-virtualenv'

Trying to make Python3 the default/preferred version

I have tried to make python3 the preferred alternative by running:

alternatives --install /usr/bin/python python /usr/bin/python3.5 2
alternatives --install /usr/bin/python python /usr/bin/python2.7 1

vim (or one of the plugins) still uses python2.7 and I get the same errors as above.

My vim version

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Jun  2 2016 10:02:17)
Included patches: 1-1868
Modified by <bugzilla@redhat.com>
Compiled by <bugzilla@redhat.com>
Huge version without GUI. 
8

The issue is that simply executing has('python') in an if-statement causes python3 to be unavailable when vim was compiled with both python/dyn and python3/dyn. The simplest solution is probably just to add something like

if exists('py2') && has('python')
elseif has('python3')
endif

to your .vimrc before Vundle loads anything. Then, if you ever need to use python 2 instead you can just start vim with vim --cmd 'let py2 = 1'.

Alternatively, I looked through your plugins and managed to find 3 which do has('python') before has('python3'):

  1. YouCompleteMe: I know you have this commented out, but it's how I stumbled across this question so it may lead someone else here. On line 36 of YouCompleteMe/plugin/youcompleteme.vim, make python3 get checked for first: elseif !has( 'python3' ) && !has( 'python' ).

  2. python-mode: If you look in python-mode/plugin/pymode.vim, around line 275 there's the "has" if-statement, you'll notice you can actually set a global variable g:_uspy to force a certain version to be used. So, either put g:_uspy = ':py3' in your .vimrc before the Vundle stuff or edit the if-statement in pymode.vim.

  3. vim-virtualenv: Same deal as YCM, reverse the conditions of the if-statement on line 10 of vim-virtualenv/plugin/virtualenv.vim.

Of course, you really only need to fix the first one that's loaded if you do it this way.

  • The if statement works brilliantly! Thanks heaps. Also for searching the vim plugins. – pandita Aug 28 '16 at 11:23
0

Assuming you have python 3 installed...

Instead of running :python, use :python3 or :py3

Try this code in Vim and it will run in python 3

:python3 print("Hello world")

For more info:

:help python3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.