【问题标题】:which and type show different paths to IPython executable in virtualenv, import fails in IPython, succeeds in standard interpreter?which 和 type 在 vi​​rtualenv 中显示 IPython 可执行文件的不同路径,在 IPython 中导入失败,在标准解释器中成功?
【发布时间】:2013-11-14 18:48:27
【问题描述】:
(myvirtualenv)[~] sam@iota $ type ipython  
ipython is hashed (/usr/local/bin/ipython)  
(myvirtualenv)[~] sam@iota $ which ipython  
/Users/sam/.virtualenvs/myvirtualenv/bin/ipython  

不管which 说什么,它都会执行/usr/local/bin/ipython

(myvirtualenv)[~] sam@iota $ ipython  
WARNING: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.  
Python 2.7.5 (default, Sep  6 2013, 17:51:18)  

IPython 1.1.0 -- An enhanced Interactive Python.

In [1]:  import novaclient
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-1-93e5b70cb5b9> in <module>()
----> 1 import novaclient

/Users/sam/.virtualenvs/myvirtualenv/lib/python2.7/site-packages/novaclient/__init__.py in <module>()
     16
     17
---> 18 __version__ = pbr.version.VersionInfo('python-novaclient').version_string()

/Users/sam/.virtualenvs/myvirtualenv/lib/python2.7/site-packages/pbr/version.pyc in version_string(self)
     70         if self.version is None:
     71             parts = []
---> 72             for part in self.release_string().split('.'):
     73                 if part[0].isdigit():
     74                     parts.append(part)

/Users/sam/.virtualenvs/myvirtualenv/lib/python2.7/site-packages/pbr/version.pyc in release_string(self)
     62         
     63         if self.release is None:
---> 64             self.release = self._get_version_from_pkg_resources()
     65
     66         return self.release

/Users/sam/.virtualenvs/myvirtualenv/lib/python2.7/site-packages/pbr/version.pyc in _get_version_from_pkg_resources(self)
     55             # installed into anything. Revert to setup-time logic.
     56             from pbr import packaging
---> 57             return packaging.get_version(self.package)
     58
     59     def release_string(self):

/Users/sam/.virtualenvs/myvirtualenv/lib/python2.7/site-packages/pbr/packaging.pyc in get_version(package_name, pre_version)
    820     if version:
    821         return version
--> 822     raise Exception("Versioning for this project requires either an sdist"
    823                     " tarball, or access to an upstream git repository."
    824                     " Are you sure that git is installed?")

Exception: Versioning for this project requires either an sdist tarball, or access to an upstream git repository. Are you sure that git is installed?

现在强制它执行/Users/sam/.virtualenvs/myvirtualenv/bin/ipython

(myvirtualenv)[~] sam@iota $ `which ipython`
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()  

为什么whichtype 指向不同的路径?为什么,如果 IPython 安装在我的 virtualenv 中,系统 IPython 会被执行,即使我的 $PATH 的顺序会指示不是这样?无论如何,为什么import novaclient 会出现一个看起来很奇怪的错误?


编辑

来自错误会话的pythonpath

(myvirtualenv)[~] sam@iota $ ipython -c 'import sys;sys.path'

['/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 'import sys;print sys.path'

['',
 '/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']

【问题讨论】:

  • 可能对于像超级用户这样的网站来说是一个更好的问题,因为我认为最终这个问题与 virtualenv 无关,或者除了 bash 之外的其他任何东西。但我不确定——在你真正知道答案之前,没有办法知道这一点(对于 OP,或者对于未来有类似问题的搜索者)。

标签: python python-2.7 virtualenv ipython openstack-nova


【解决方案1】:

为什么 which 和 type 指向不同的路径?

如果你的 shell 已经对 ipython 的路径进行了哈希处理,type 会给你哈希路径而不搜索 $PATH,而which 将忽略哈希路径并进行搜索。

假设您的 shell 是 bashhash 命令(不带参数)将显示散列路径,或者您可以查看 BASH_CMDS 关联数组。但是你已经知道ipython 是经过哈希处理的,因为type 刚刚告诉过你。

通常你可以通过启动一个新的 shell 来解决这个问题,使用 hash -r 命令(擦除缓存而不重新启动其他任何东西)或hash -d ipython(从缓存中删除 ipython,但请确保你也没有,例如,python 在缓存中)。使用不同的 shell,这两个命令可能不同(例如,在 *csh 和 zsh 中,分别是 rehashrehash ipython),但启动一个新的 shell 应该总是有效的。

有关更多详细信息,请参阅 bash 参考手册中的 Command Search and Execution 或系统上的 bash(1) 联机帮助页,或者如果您在 google 上搜索“bash hash”之类的内容,就会出现大量易于理解的博客文章缓存”(如this one)。

为什么,如果 IPython 安装在我的 virtualenv 中,系统 IPython 会被执行

如果您的 shell 已对 ipython 的路径进行哈希处理,ipython 将从哈希路径运行,而不搜索 $PATH

同样,启动一个新的 shell 等应该可以解决这个问题。

无论如何,为什么 import novaclient 会出现看起来很奇怪的错误?

很可能它是从您的环境中获取PYTHONPATH,这会导致 venv 的 site-packages 和 dist-packages 成为系统 Python 的,并且这些位置中的包不适用于系统 Python。

您可以通过打印出sys.path 来验证这一点。但它抱怨/Users/sam/.virtualenvs/myvirtualenv/lib/python2.7/site-packages/ 中的文件这一事实意味着该路径必须在sys.path 上。而且(因为您从不同的目录运行)不仅因为默认的. 正在选择它,而且因为它明确存在。所以,sys.path 可以告诉你的只是它是绝对路径还是相对路径。

【讨论】:

  • 很好的答案,谢谢。 it must be happening 是什么意思?
  • @smlstvnh:我的意思是这条路径一定会以某种方式到达sys.path。重读,你是对的,不是很清楚。我会修复它,感谢您指出。
猜你喜欢
  • 1970-01-01
  • 2012-03-09
  • 2016-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
  • 2016-08-28
相关资源
最近更新 更多