哪个和类型在virtualenv中显示IPython可执行文件的不同路径,在IPython中导入失败,在标准解释器中导入成功? 无论哪个说了什么,它都执行/usr/local/bin/ipython
为什么哪个和类型在virtualenv中显示IPython可执行文件的不同路径,在IPython中导入失败,在标准解释器中导入成功? 无论哪个说了什么,它都执行/usr/local/bin/ipython,python,python-2.7,virtualenv,ipython,openstack-nova,Python,Python 2.7,Virtualenv,Ipython,Openstack Nova,为什么哪个和类型指向不同的路径?为什么,如果我的virtualenv中安装了IPython,那么系统IPython会被执行,即使我的$PATH的顺序表明不是这样?在任何情况下,import-novaclient为什么会突然出现一个非常奇怪的错误 编辑 来自错误会话的pythonpath (myvirtualenv)[~]sam@iota$ipython-c'导入系统;系统路径“ Python 2.7.5 (default, Sep 6 2013, 17:51:18) [GCC 4.2.1 C
哪个
和类型
指向不同的路径?为什么,如果我的virtualenv中安装了IPython,那么系统IPython会被执行,即使我的$PATH
的顺序表明不是这样?在任何情况下,import-novaclient
为什么会突然出现一个非常奇怪的错误
编辑 来自错误会话的pythonpath
(myvirtualenv)[~]sam@iota$ipython-c'导入系统;系统路径“
Python 2.7.5 (default, Sep 6 2013, 17:51:18)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.24)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import novaclient
>>> print "all is well here"
all is well here
>>> exit()
['/Users/sam/.virtualenvs/myvirtualenv/lib/python2.7/site-packages',
'',
'/usr/local/bin',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-1.1-py2.7.egg',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.4.1-py2.7.egg',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/Library/Python/2.7/site-packages',
'/usr/local/lib/python2.7/site-packages',
'/usr/local/lib/python2.7/site-packages/IPython/extensions']
成功会话中的pythonpath
(myvirtualenv)[~]sam@iota$python-c'导入系统;打印系统路径“
Python 2.7.5 (default, Sep 6 2013, 17:51:18)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.24)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import novaclient
>>> print "all is well here"
all is well here
>>> exit()
['/Users/sam/.virtualenvs/myvirtualenv/lib/python2.7/site-packages',
'',
'/usr/local/bin',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-1.1-py2.7.egg',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.4.1-py2.7.egg',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/Library/Python/2.7/site-packages',
'/usr/local/lib/python2.7/site-packages',
'/usr/local/lib/python2.7/site-packages/IPython/extensions']
为什么哪个和类型指向不同的路径
如果您的shell对ipython
的路径进行了散列,则type
将在不搜索$path
的情况下为您提供散列路径,而将忽略散列路径并进行搜索
假设您的shell是bash
,则hash
命令(不带参数)将显示哈希路径,或者您可以查看bash\u CMDS
关联数组。但是您已经知道,ipython
是散列的,因为type
刚刚告诉您
通常,您可以通过使用hash-r
命令(在不重新启动任何其他命令的情况下擦除缓存)或hash-d ipython
(从缓存中仅移除ipython
,但确保缓存中没有,例如python
)来修复此问题。对于不同的shell,这两个命令可能不同(例如,在*csh和zsh中,分别是rehash
和rehash ipython
),但启动新shell应该始终有效
有关更多详细信息,请参阅bash参考手册中的,或系统上的bash(1)
manpage,或者如果您使用谷歌搜索“bash哈希缓存”(like)之类的内容,将显示许多易于理解的博客文章
为什么,如果我的virtualenv中安装了IPython,那么系统IPython会被执行
如果shell对ipython
的路径进行了散列,则ipython
将从散列路径运行,而不搜索$path
同样,启动一个新的shell等,应该可以解决这个问题
在任何情况下,为什么导入novaclient会出现一个看起来非常奇怪的错误
最有可能的是,它从您的环境中获得了PYTHONPATH
,这导致venv的站点包和dist包成为系统Python的包,而这些位置的包与系统Python不兼容
您可以通过打印出sys.path
来验证这一点。但它抱怨/Users/sam/.virtualenvs/myvirtualenv/lib/python2.7/site packages/
中的文件,这意味着该路径必须位于sys.path
上。而且(因为您是从不同的目录运行的)不仅仅是因为默认的
正在拾取它,而是因为它显式地在那里。所以,sys.path
能告诉你的只是它是绝对路径还是相对路径。回答得好,谢谢。你所说的它一定正在发生是什么意思?@smlstvnh:我的意思是,这条路径一定以某种方式进入了系统路径。重读一遍,你是对的,它不是很清楚。我会解决这个问题,谢谢你指出。对于像超级用户这样的网站来说,这可能是一个更好的问题,因为我认为问题最终与virtualenv或bash无关。但我不确定在你真正知道答案之前,没有办法知道这一点(无论是对于OP,还是对于未来有类似问题的搜索者)。
Python 2.7.5
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
In [1]: import novaclient
In [2]: print "all is well here"
all is well here
(myvirtualenv)[~] sam@iota $ echo $PATH
/Users/sam/.virtualenvs/myvirtualenv/bin:/usr/local/sbin:/usr/local/lib:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
(myvirtualenv)[~] sam@iota $ python
Python 2.7.5 (default, Sep 6 2013, 17:51:18)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.24)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import novaclient
>>> print "all is well here"
all is well here
>>> exit()
['/Users/sam/.virtualenvs/myvirtualenv/lib/python2.7/site-packages',
'',
'/usr/local/bin',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-1.1-py2.7.egg',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.4.1-py2.7.egg',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/Library/Python/2.7/site-packages',
'/usr/local/lib/python2.7/site-packages',
'/usr/local/lib/python2.7/site-packages/IPython/extensions']
['',
'/Users/sam/.virtualenvs/projectx-dev-2/bin',
'/Users/sam/.virtualenvs/projectx-dev-2/src/pyrax',
'/Users/sam/.virtualenvs/projectx-dev-2/lib/python27.zip',
'/Users/sam/.virtualenvs/projectx-dev-2/lib/python2.7',
'/Users/sam/.virtualenvs/projectx-dev-2/lib/python2.7/plat-darwin',
'/Users/sam/.virtualenvs/projectx-dev-2/lib/python2.7/plat-mac',
'/Users/sam/.virtualenvs/projectx-dev-2/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/sam/.virtualenvs/projectx-dev-2/lib/python2.7/lib-tk',
'/Users/sam/.virtualenvs/projectx-dev-2/lib/python2.7/lib-old',
'/Users/sam/.virtualenvs/projectx-dev-2/lib/python2.7/lib-dynload',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/sam/.virtualenvs/myvirtualenv/lib/python2.7/site-packages',
'/Users/sam/.virtualenvs/myvirtualenv/lib/python2.7/site-packages/IPython/extensions']