为什么vim中的sys.path与shell中的sys.path不同?

时间:2018-03-01 14:23:59

标签: python vim macvim

如果我在vim中执行命令execute "python import sys; print(sys.path)",我会得到以下路径列表:

['/Users/<myuser>/.vim/bundle/python-mode/pymode/libs', '/Users/<myuser>/.vim/bundle/python-mode', '/Users/<myuser>', '/Users/<myuser>/Library/Python/2.7/lib/python/site-packages', '/usr/local/Cellar/python/2.7
.14_3/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/usr/local/Cellar/python/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/usr/local/Cellar/python/2.7.14_3/Framework
s/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/usr/local/Cellar/python/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/usr/local/Cellar/python/2.7.14_3/Framew
orks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/usr/local/Cellar/python/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/usr/local/Cellar/pytho
n/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/usr/local/Cellar/python/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/usr/local/Cellar/pyt
hon/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', '_vim_path_']

但如果我在shell中使用bpython进行尝试,我会得到一个不同的列表:

>>> import sys
>>> sys.path
['', '/usr/local/bin', '/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg', '/Library/Python/2.7/site-packages/percol-0.2.1-py2.7.egg', '/Library/Python/2.7/site-packages/cmigemo-0.1.6-py2.7.egg', '
/Library/Python/2.7/site-packages/six-1.11.0-py2.7.egg', '/Library/Python/2.7/site-packages', '/Users/<myuser>', '/Users/<myuser>/Library/Python/2.7/lib/python/site-packages', '/System/Library/Frameworks/Py
thon.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-darw
in', '/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/Lib
rary/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/Versio
ns/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']

使用python,列表为:

['', '/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg', '/Library/Python/2.7/site-packages/percol-0.2.1-py2.7.egg', '/Library/Python/2.7/site-packages/cmigemo-0.1.6-py2.7.egg', '/Library/Python/2.7/site-packages/six-1.11.0-py2.7.egg', '/Library/Python/2.7/site-packages', '/Users/shizhz', '/Users/shizhz/Library/Python/2.7/lib/python/site-packages', '/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', '/Library/Python/2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']

这让我困扰的原因是我在vim中设置了python-mode的python环境,但由于vim中sys.path的python不包含所有python库的完整列表路径(例如/Library/Python/下的目录),这使得自动完成不适用于某些第三方库。例如requests位于我的计算机上的/Library/Python/2.7/site-packages/,但是当我尝试在键入时触发自动完成时,没有任何操作(即使手动使用C-X C-O),并且当我执行时execute "python showdbg()",调试消息为:

DBG: source: import requests
class _PyCmplNoType:
    def __getattr__(self,name):
        return None

DBG: parser: <type 'exceptions.ImportError'>, No module named requests
DBG: locals: <type 'exceptions.ImportError'>, No module named requests [import requests]
DBG: get_completions('request.','')
DBG: completion: <type 'exceptions.NameError'>,name 'request' is not defined [stmt='request']

但是,如果我将/Library/Python/2.7/site-packages/追加到sys.path,它就会完美无缺。那我的问题

  • 为什么两个列表不同,以及vim如何进入sys.path?
  • 在vim中添加所有python库位置的最佳做法是什么?

0 个答案:

没有答案