Resolve Vim's Python interface path issue

If Vim's Python interface path settings aren't working properly, completion won't work with Python-based plugins, such as jedi-vim. Or when using vim-geeknote

function Vim_An error was detected while processing GeeknoteToggle:
Line 4:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/tmsanrinsha/.vim/bundle/vim-geeknote/plugin/vim_geeknote.py", line 4, in <module>
    from explorer import Explorer
  File "/Users/tmsanrinsha/.vim/bundle/vim-geeknote/plugin/explorer.py", line 4, in <module>
    from view   import *
  File "/Users/tmsanrinsha/.vim/bundle/vim-geeknote/plugin/view.py", line 3, in <module>
    from enml  import *
  File "/Users/tmsanrinsha/.vim/bundle/vim-geeknote/plugin/enml.py", line 5, in <module>
    from geeknote.out import *

I get the error.

This happens because the Python referenced by Vim's Python interface is different from the Python you intended.

On a Mac, this happens when you install Python with Homebrew and use MacVim-KaoriYa.

Check version and sys.path on the command line

~$ python -c 'import sys; print(sys.version); print(sys.path);'
2.7.9 (default, Dec 19 2014, 06:00:59)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)]
['', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-8.2.1-py2.7.egg', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy-1.10.0.dev0+fb898ce-py2.7-macosx-10.10-x86_64.egg', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/geeknote-0.2a0-py2.7.egg', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/thrift-0.9.2-py2.7-macosx-10.10-x86_64.egg', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/SQLAlchemy-0.9.8-py2.7-macosx-10.10-x86_64.egg', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/html2text-2015.2.18-py2.7.egg', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/evernote-1.25.0-py2.7.egg', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/oauth2-1.5.211-py2.7.egg', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/httplib2-0.9-py2.7.egg', '/Library/Python/2.7/site-packages/markdown2-2.1.0-py2.7.egg', '/usr/local/lib/python2.7/site-packages/setuptools-8.2.1-py2.7.egg', '/usr/local/lib/python2.7/site-packages/numpy-1.10.0.dev0+fb898ce-py2.7-macosx-10.10-x86_64.egg', '/usr/local/lib/python2.7/site-packages/geeknote-0.2a0-py2.7.egg', '/usr/local/lib/python2.7/site-packages/thrift-0.9.2-py2.7-macosx-10.10-x86_64.egg', '/usr/local/lib/python2.7/site-packages/SQLAlchemy-0.9.8-py2.7-macosx-10.10-x86_64.egg', '/usr/local/lib/python2.7/site-packages/html2text-2015.2.18-py2.7.egg', '/usr/local/lib/python2.7/site-packages/evernote-1.25.0-py2.7.egg', '/usr/local/lib/python2.7/site-packages/oauth2-1.5.211-py2.7.egg', '/usr/local/lib/python2.7/site-packages/httplib2-0.9-py2.7.egg', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Python/2.7/site-packages', '/usr/local/lib/python2.7/site-packages']

But if you check with Vim

:python print sys.version; print sys.path
2.7.6 (default, Sep  9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)]
['/Library/Python/2.7/site-packages/markdown2-2.1.0-py2.7.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2
.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/
2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages', '_vim_path_', '/Users/tmsanrinsha/.vim/bundle/vim-geeknote/plugin', '/Users/tmsanrinsha/.vim/bundle/vim-geeknote/plugin']

It turns out that version and sys.path are different. If you try using standard Python for Mac

~$ /usr/bin/python -c 'import sys; print(sys.version); print(sys.path);'
2.7.6 (default, Sep  9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)]
['', '/Library/Python/2.7/site-packages/markdown2-2.1.0-py2.7.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages']

The sys.path is slightly different, but the version is the same, and you can see that Vim uses this Python.

[Macvim KaoriYa Readme + perl / + python / + python3 / + ruby / + lua](https://code.google.com/p/macvim-kaoriya/wiki/Readme#+perl_/+python/_ + python3_ / _ + ruby_ / _ + lua), the setting when you want to use dylib such as Homebrew is written, but because there is no file /usr/local/lib/libpython2.7.dylib, Imitate the setting of $ PYTHON3_DLL

let $PYTHON_DLL = "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/Python"

And check version and sys.path again

2.7.9 (default, Dec 19 2014, 06:00:59)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)]
['/Library/Python/2.7/site-packages/markdown2-2.1.0-py2.7.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2
.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/
2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages', '_vim_path_', '/Users/tmsanrinsha/.vim/bundle/vim-geeknote/plugin']

The version has changed, but sys.path hasn't changed. So I decided to call Python from vim using the system function and add it to sys.path.

:!which python

Try it, make sure you have a Python PATH, and put it in vimrc

let s:python_path = system('python -', 'import sys;sys.stdout.write(",".join(sys.path))')

python <<EOM
import sys
import vim

python_paths = vim.eval('s:python_path').split(',')
for path in python_paths:
    if not path in sys.path:
        sys.path.insert(0, path)
EOM

If you write, you can see that sys.path has been added.

However, with this, even if you do not need the Python interface, system will be called every time you start Vim, so set it only when using a plugin that requires a Python interface using NeoBundleLazy etc.

Finally set it like this.

NeoBundleLazy 'davidhalter/jedi-vim', {
\   'autoload': {'filetypes': ['python']}
\}

:

"python sys.path setting" {{{
if filereadable('/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/Python')
    let $PYTHON_DLL = "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/Python"
endif

function! s:set_python_path()
    let s:python_path = system('python -', 'import sys;sys.stdout.write(",".join(sys.path))')

    python <<EOT
import sys
import vim

python_paths = vim.eval('s:python_path').split(',')
for path in python_paths:
    if not path in sys.path:
        sys.path.insert(0, path)
EOT
endfunction
" }}}

let s:hooks = neobundle#get_hooks("jedi-vim")
function! s:hooks.on_source(bundle)
    call s:set_python_path()

    :

endfunction

"setting the path option
autocmd MyVimrc FileType python
\   let &l:path = system('python -', 'import sys;sys.stdout.write(",".join(sys.path))')

Finally, I added the setting of Vim's path option. This is used for include completion of neocomplete & neocomplcache and for directories that are searched when using gf.

How to fetch Python sys.path is described in How to use include completion of neocomplcache.vim-forever incomplete I referred to.

Recommended Posts

Resolve Vim's Python interface path issue
Use embeddable Python as Vim's Python 3 interface
Python issue tracker Trac
Naturally sort Path in Python
[Python] Theremin interface with mouse
Register redmine issue from Python