为什么easy_install能够正确修改Python模块加载路径,而pip和.tar.gz源代码却不能?

为什么easy_install能够正确修改Python模块加载路径,而pip和.tar.gz源代码却不能?,python,macos,pip,easy-install,setup.py,Python,Macos,Pip,Easy Install,Setup.py,我正在运行OS X 10.9.4的Mac笔记本电脑上使用苹果工厂安装的Python 2.7.5发行版 此特定发行版附带了几个流行Python附加程序包的“工厂加载”版本,例如,其中许多位于目录/System/Library/Frameworks/Python.framework/versions/2.7/Extras/lib/Python中 我发现我能够更新这些预加载软件包的版本号,但前提是我使用Apple factory安装的更新实用程序,/usr/bin/easy\u install。如果我

我正在运行OS X 10.9.4的Mac笔记本电脑上使用苹果工厂安装的Python 2.7.5发行版

此特定发行版附带了几个流行Python附加程序包的“工厂加载”版本,例如,其中许多位于目录
/System/Library/Frameworks/Python.framework/versions/2.7/Extras/lib/Python

我发现我能够更新这些预加载软件包的版本号,但前提是我使用Apple factory安装的更新实用程序,
/usr/bin/easy\u install
。如果我改为尝试使用pip或直接从.tar.gz源代码使用
python setup.py install
进行更新,那么安装会以一种非常特殊的方式失败,我将在下面详细描述。我想解决这个问题,以便能够使用我喜欢的任何方法更新Python包

首先,描述我认为“正确”的行为:当我使用easy_install更新numpy时,它会在
/Library/Python/2.7/site packages
下创建一个新的子目录,
numpy-1.9.0-py2.7-macosx-10.9-intel.egg
,例如:

$> ls -l /Library/Python/2.7/site-packages | grep numpy
drwxr-xr-x   4 root  wheel     136 Sep 11 10:21 numpy-1.9.0-py2.7-macosx-10.9-intel.egg
$> ls -l /Library/Python/2.7/site-packages | grep numpy
drwxr-xr-x  35 root  wheel    1190 Sep 11 10:24 numpy
drwxr-xr-x   8 root  wheel     272 Sep 11 10:24 numpy-1.9.0.dist-info
然后easy_install实用程序还修改Python模块加载路径,以便“新”numpy安装优先于出厂默认版本;e、 例如,在python中,它如下所示:

In [1]: from pprint import pprint

In [2]: import sys

In [3]: pprint(sys.path)
['',
 '/usr/local/bin',
 '/Library/Python/2.7/site-packages/PyVCF-0.6.7-py2.7-macosx-10.9-intel.egg',
 '/Library/Python/2.7/site-packages/distribute-0.7.3-py2.7.egg',
 '/Library/Python/2.7/site-packages/setuptools-3.3-py2.7.egg',
 '/Library/Python/2.7/site-packages/PyYAML-3.11-py2.7-macosx-10.9-intel.egg',
 '/Library/Python/2.7/site-packages/wheel-0.24.0-py2.7.egg',
 '/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg',
 '/Library/Python/2.7/site-packages/numpy-1.9.0-py2.7-macosx-10.9-intel.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',
 '/Library/Python/2.7/site-packages/IPython/extensions',
 '/Users/stachyra/.ipython']

In [4]: import numpy

In [5]: numpy.__file__
Out[5]: '/Library/Python/2.7/site-packages/numpy-1.9.0-py2.7-macosx-10.9-intel.egg/numpy/__init__.pyc'

In [6]: numpy.__version__
Out[6]: '1.9.0'
如果我卸载numpy,例如使用
sudo-pip-uninstall-numpy
(我实际上不知道如何使用easy\u-install进行卸载),它会导致numpy从
/Library/Python/2.7/site-packages
中删除,而出厂时安装了“default”
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/Python
中的版本仍然保留

为了观察“不正确”的安装行为,我可以重新升级工厂加载的numpy版本,这次使用
sudo pip install-U numpy
。这将导致在
/Library/Python/2.7/site packages
中创建一对新目录,例如:

$> ls -l /Library/Python/2.7/site-packages | grep numpy
drwxr-xr-x   4 root  wheel     136 Sep 11 10:21 numpy-1.9.0-py2.7-macosx-10.9-intel.egg
$> ls -l /Library/Python/2.7/site-packages | grep numpy
drwxr-xr-x  35 root  wheel    1190 Sep 11 10:24 numpy
drwxr-xr-x   8 root  wheel     272 Sep 11 10:24 numpy-1.9.0.dist-info
但是,当我查看python中的加载路径,然后加载numpy并查看numpy的版本号和文件时,我得到了以下结果:

In [1]: from pprint import pprint

In [2]: import sys

In [3]: pprint(sys.path)
['',
 '/usr/local/bin',
 '/Library/Python/2.7/site-packages/PyVCF-0.6.7-py2.7-macosx-10.9-intel.egg',
 '/Library/Python/2.7/site-packages/distribute-0.7.3-py2.7.egg',
 '/Library/Python/2.7/site-packages/setuptools-3.3-py2.7.egg',
 '/Library/Python/2.7/site-packages/PyYAML-3.11-py2.7-macosx-10.9-intel.egg',
 '/Library/Python/2.7/site-packages/wheel-0.24.0-py2.7.egg',
 '/Library/Python/2.7/site-packages/pip-1.5.6-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',
 '/Library/Python/2.7/site-packages/IPython/extensions',
 '/Users/stachyra/.ipython']

In [4]: import numpy

In [5]: numpy.__file__
Out[5]: '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/__init__.pyc'

In [6]: numpy.__version__
Out[6]: '1.6.2'
如您所见,基于上述情况,似乎pip无法使用最新更新的numpy版本更新sys.path变量,而不是加载最新版本(numpy 1.9.0),python将加载较旧的出厂默认版本1.6.2

除了上述两种方法之外,我还可以使用
sudo python setup.py install
直接从.tar.gz文件安装numpy,在这种情况下,我得到的结果与使用pip时非常相似

所以我的问题是,首先,到底是什么被打破了?除了通常的默认版本(
/Library/Python/2.7/site packages
)之外,它是否是Apple factory默认版本的Python,因为它已经将预加载的Python包安装到了其他一些非标准目录区域(
/System/Library/Frameworks/Python.Frameworks/Python.framework/Versions/2.7
)?还是pip和.tar.gz源文件未能正确覆盖Apple factory默认安装

还有,我问题的第二部分:如何解决这个问题,以便将来如果我愿意,可以使用pip和.tar.gz?FWIW,如果答案包括卸载Apple factory默认设置,那么我不希望进入
/System/Library/Frameworks
目录,开始手动删除东西,因为这似乎是破坏东西的潜在方法


还有一件事:我知道我可以通过简单地使用其他一些替代发行版(如or)来回避整个问题,但是,对我来说,“正确地”解决这个问题似乎更干净、更优雅如果可能的话。

easy\u install使用一种方法,该方法通过munges sys.path将已安装的项目注入
sys.path
的前面,而另一方面,pip只是将它们放在Python默认查找的目录中(
site packages
)。这几乎总是有效的,除非Python的源代码修改了Python,比如Apple必须在
站点包
目录之前插入自己的预装项目

有人反对pip提出解决这个问题的方案,但从根本上说,问题是因为苹果打破了默认的
sys.path
顺序