1

I'm working on a server that has various different python installations set up. At any one time, only one will be accessable and working, depending on what I am working on. Some are python2, some python3. They are not all located in the same place. Not my arrangement, and I cannot change it.

Vim understandably has trouble finding the python install, and frequently coughs up a litany of errors when the plugins don't run properly. The plugins are nice to have, but I don't need them. Basic vim is actually still a very nice editor. I would like to get rid of the errors, because there are pages of them.

Is there a way I can put a check in my vimrc that will stop the plugins from loading if a valid python installation is not found?

I'm using vundle if that makes any difference.

This answer is nearly what I need. I just need a test for a valid python install, rather than a version of vim.

3
  • 1
    Maybe a try-catch with a :py statement?
    – D. Ben Knoble
    Jan 18, 2022 at 21:54
  • @D.BenKnoble good thinking, maybe for some of them the package load itself could go in the try catch, provided the errors come out immediately. I will see if I can get something working.
    – Clumsy cat
    Jan 19, 2022 at 9:18
  • 1
    @D.BenKnoble I've found a solution that used your suggestion. I'd love any critique you have on it, because I'm entirely new to vimscript. In particular, some of those if statements really want to be else if, but I couldn't figure out how to do that.
    – Clumsy cat
    Jan 21, 2022 at 9:06

1 Answer 1

0

So, to follow up for anyone else who finds a need to do something like this. This is what I put in my .vimrc;


" decide which version of python we have available
" note that prefixing the variable with s: means that it's
" local to this script, the vimrc
try
    let s:pythonversion = pyeval('sys.version_info[0]')
catch
    " python didn't work at all
    let s:pythonversion = 0
endtry


if s:pythonversion ==? 0
    let s:haspython = 0
    let s:haspython3 = 0
endif
if s:pythonversion ==? 2
    let s:haspython = 1
    let s:haspython3 = 0
endif
if s:pythonversion ==? 3
    let s:haspython = 1
    let s:haspython3 = 1
endif

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'

" add all your plugins here (note older versions of Vundle
" used Bundle instead of Plugin)

" ale for syntax checking
" no python requirements
Plugin 'w0rp/ale'

" some plugin that requires python3
if s:haspython3
    Plugin 'google/vim-maktaba'
    Plugin 'google/vim-coverage'
endif
 
" variation, if all that is needed is python2
if s:haspython
    Plugin 'ivanov/vim-ipython'
endif

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required

This seems to get rid of any errors from missing python. The tricky bit was working out what the requirements on plugins actually are, lots of them don't seem to display that information anywhere.

1
  • 1
    Thoughts: yes, elseif would work, but really I think you want :let s:haspython = s:pythonversion >= 2 and let s:haspython3 = s:pythonversion is# 3, or maybe let s:haspython = (s:pythonversion is# 2) || (s:pythonversion is# 3)
    – D. Ben Knoble
    Jan 21, 2022 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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