34
31

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VimのPythonインターフェースのパスの問題を解消する

Last updated at Posted at 2015-03-14

VimのPythonインターフェースのパスの設定がうまくいってないと、Pythonを使うプラグイン、例えばjedi-vimを使って補完がうまく行かなかったり、vim-geeknoteを使った時、

function Vim_GeeknoteToggle の処理中にエラーが検出されました:
行    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 *

というエラーが出たりする。

これはVimのPythonインターフェースが参照するPythonと、自分が意図したPythonが違うために起こる。

Macの場合だとHomebrewでPythonをインストールして、MacVim-KaoriYaを使った時にこのような状況になる。

コマンドラインでversionとsys.pathを確かめてみると

~$ 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']

となるが、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']

となって、versionとsys.pathが違うことがわかる。Macの標準のPythonを使ってみると

~$ /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']

sys.pathは若干違うが、versionは同じで、VimはこちらのPythonを使っていることがわかる。

Macvim KaoriYa のReadmeの+perl / +python / +python3 / +ruby / +luaを見るとHomebrewなどのdylibを使用したい場合の設定が書いてあるが、/usr/local/lib/libpython2.7.dylibというファイルが無いため、$PYTHON3_DLLの設定を真似て

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

と設定し、再度versionと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)]
['/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']

となり、versionは変わったが、sys.pathの方は変わらない。そこでvimからsystem関数を使って、Pythonを呼び出し、sys.pathに追加することにする。

:!which python

してみて、PythonのPATHがあっていることを確かめて、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

と書いておくと、sys.pathが追加されていることがわかる。

ただこれだと、Pythonインターフェースが必要なくても、Vimを起動するたびにsystemが呼ばれるため、NeoBundleLazy等を使ってPythonインターフェースが必要なプラグインを使う時だけ設定するようにする。

最終的にこんな感じに設定する。

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

:

" pythonのsys.pathの設定 " {{{
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

" pathオプションの設定
autocmd MyVimrc FileType python
\   let &l:path = system('python -', 'import sys;sys.stdout.write(",".join(sys.path))')

ついでに、最後にVimのpathオプションの設定を追加した。こちらはneocomplete & neocomplcacheのインクルード補完や、gfを使った時に検索されるディレクトリに使われる。

Pythonのsys.pathを取ってくる方法はneocomplcache.vim のインクルード補完の使い方 - 永遠に未完成に書いてある方法を参考にした。

34
31
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
31

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?