Skip to content

Commit

Permalink
neovim: Use local (venv, conda) python3 as neovim host if appropriate
Browse files Browse the repository at this point in the history
Previously, neovim's host python3 is configured in the order of
system python (/usr/local/bin/python3, /usr/bin/python3) and then
local python3 from $PATH (e.g. virtualenv or anaconda). This means
as long as system python exists, it always becomes neovim's host python.

From now, local python3 is more prioritized given that 'neovim' package
is installed (otherwise, use system python as default host python
so that it doesn't force users to have neovim package installed on it).

This will allow us to use local python3 as a neovim's host python
if we want (by installing 'neovim' package), which could be quite
useful in some use cases such as numirias/semshi#19 .
  • Loading branch information
wookayin committed Sep 20, 2018
1 parent f56351e commit 189941d
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions nvim/init.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,27 @@ if empty(glob(g:python_host_prog))
let g:python_host_prog = '/usr/bin/python'
endif

let g:python3_host_prog = '/usr/local/bin/python3'
if empty(glob(g:python3_host_prog))
" Fallback if not exists
let g:python3_host_prog = '/usr/bin/python3'
endif
if empty(glob(g:python3_host_prog)) && executable("python3")
" Fallback to local/venv python3 if not exists
let g:python3_host_prog = substitute(system("which python3"), '\n\+$', '', '')

let g:python3_host_prog = ''

if executable("python3")
" get local python from $PATH (virtualenv/anaconda or system python)
let s:python3_local = substitute(system("which python3"), '\n\+$', '', '')
" detect whether neovim package is installed
let s:python3_neovim_path = substitute(system("python3 -c 'import neovim; print(neovim.__path__)' 2>/dev/null"), '\n\+$', '', '')
if !empty(s:python3_neovim_path)
" neovim available, use it as a host python3
let g:python3_host_prog = s:python3_local
endif
else
let s:python3_local = ''
endif

" Fallback to system python3 (if not exists)
if empty(glob(g:python3_host_prog)) | let g:python3_host_prog = '/usr/local/bin/python3' | endif
if empty(glob(g:python3_host_prog)) | let g:python3_host_prog = '/usr/bin/python3' | endif
if empty(glob(g:python3_host_prog)) | let g:python3_host_prog = s:python3_local | endif

" VimR support {{{
" @see https://github.com/qvacua/vimr/wiki#initvim
if has('gui_vimr')
Expand Down

0 comments on commit 189941d

Please sign in to comment.