Beheben Sie das Problem mit dem Python-Schnittstellenpfad von Vim

Wenn die Einstellungen für den Python-Schnittstellenpfad von Vim nicht ordnungsgemäß funktionieren, funktioniert die Fertigstellung nicht mit Python-basierten Plug-Ins wie jedi-vim. Oder wenn Sie [vim-geeknote] verwenden (https://github.com/neilagabriel/vim-geeknote)

function Vim_Bei der Verarbeitung von GeeknoteToggle wurde ein Fehler festgestellt:
Zeile 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 *

Ich bekomme den Fehler.

Dies geschieht, weil sich der Python, auf den die Python-Oberfläche von Vim verweist, von dem von mir beabsichtigten Python unterscheidet.

Im Falle eines Mac tritt diese Situation auf, wenn Python mit Homebrew installiert und MacVim-KaoriYa verwendet wird.

Überprüfen Sie die Version und sys.path in der Befehlszeile

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

Wenn Sie sich jedoch bei Vim erkundigen

: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']

Es stellt sich heraus, dass version und sys.path unterschiedlich sind. Wenn Sie versuchen, das Standard-Python für Mac zu verwenden

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

Der sys.path ist etwas anders, aber die Version ist dieselbe, und Sie können sehen, dass Vim diesen Python verwendet.

[Macvim KaoriYa Readme + perl / + python / + python3 / + ruby / + lua](https://code.google.com/p/macvim-kaoriya/wiki/Readme#+perl_/+python/_ + python3_ / _ + ruby_ / _ + lua) zeigt die Einstellungen an, wenn Sie Dylib wie Homebrew verwenden möchten, aber weil es keine Datei mit dem Namen / usr / local / lib / libpython2.7.dylib gibt Imitieren Sie die Einstellung von $ PYTHON3_DLL

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

Und überprüfen Sie die Version und sys.path erneut

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

Die Version hat sich geändert, aber sys.path hat sich nicht geändert. Deshalb habe ich beschlossen, Python über die Systemfunktion von vim aus aufzurufen und zu sys.path hinzuzufügen.

:!which python

Probieren Sie es aus, stellen Sie sicher, dass Sie den Python-Pfad haben, und legen Sie ihn in vimrc ab

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

Wenn Sie schreiben, können Sie sehen, dass sys.path hinzugefügt wurde.

In diesem Fall wird das System bei jedem Start von Vim aufgerufen, auch wenn die Python-Schnittstelle nicht erforderlich ist. Legen Sie es daher nur fest, wenn Sie ein Plug-In verwenden, für das eine Python-Schnittstelle mit NeoBundleLazy usw. erforderlich ist.

Stellen Sie es schließlich so ein.

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

:

"Python Sys.Pfadeinstellung" {{{
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

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

Schließlich habe ich die Einstellung der Pfadoption von Vim hinzugefügt. Dies wird zum Einschließen von neocomplete & neocomplcache und für Verzeichnisse verwendet, die bei Verwendung von gf durchsucht werden.

Die Methode zum Abrufen des sys.path von Python wird unter Verwendung des Abschlusses von neocomplcache.vim-forever unvollständig beschrieben. Wurde verwiesen.

Recommended Posts

Beheben Sie das Problem mit dem Python-Schnittstellenpfad von Vim
Verwenden Sie einbettbares Python als Python 3-Oberfläche von Vim
Issue Management Tool Trac von Python
Sortieren Sie den Pfad natürlich in Python
[Python] Termin mit Mausschnittstelle
Registrieren Sie das Redmine-Problem von Python