(对缺少或正确的链接深表歉意;系统不允许我添加两个以上的链接。)
不幸的是,我经历了惨痛的教训,才知道不应该弄乱默认的 Python Mac OS X(特别是 10.6.8)中的安装。
使用 2.6.6 的 python.org 安装程序后 (http://www.python .org/getit/releases/2.6.6/) 和 2.5.4 (http://www.python.org/getit/releases/2.5.4/),我有比Apple提供的更成熟的Python版本(这对于开发来说非常有用),但是有损坏的核心系统功能(这对其他一切都不利。)到目前为止,最明显的中断是在尝试运行 namebench 时(https://code.google.com/p/namebench/)、Blink (http:// icanblink.com/)和 Mercurial (https://www.mercurial-scm.org/< /a>)。据我所知,这取决于路径。
在默认安装中,路径应类似于此中概述的内容问题。相反,我的看起来像这样:(
$ /usr/bin/python2.6 -V
Python 2.6.6
$ which python
/usr/bin/python
$ python
Python 2.6.6 (r266:84374, Aug 31 2010, 11:00:51)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> for i in sys.path:
... print i
...
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pip-1.0.2-py2.6.egg
/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages
这是 2.5 的类似故事,但为了简单起见,我将坚持使用 2.6。)
问题似乎是不包含:
- /Library/Python/2.6/site-packages/* .egg
- /Library/Python/2.6/site-packages
- /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6
- /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python
这解释了为什么 Blink 和 namebench 找不到 PyObjC (PyObjC 是否预安装在 OSX SL 上?)并且 Mercurial 找不到它的模块(在 /Library/Python/2.6 中)
Python bug 跟踪器中的问题 #4865 部分解决了这个问题特定问题,此后已在 2.7 及更高版本中修复。但因为它是“功能请求而不是错误修复”,所以尚未向后移植到 2.5 和 2.6。即便如此,查看已提交的修复(http://hg.python.org/lookup/r70778),我不确定它是否解决了缺乏对“Extras”目录的引用的问题。
据我了解,我可以通过手动更改 site.py 来手动添加 Python 路径。我还可以使用 PYTHONPATH 环境变量。我不想通过更改 site.py 造成任何进一步的损害,并且更改 PYTHONPATH 仅对使用我的用户帐户从 shell 运行的脚本/应用程序有效。
我可以让这些更新版本的 Python 引用与默认框架安装相同的路径吗?如果是这样,最好的方法是什么?如果没有,是否有公认的回滚到系统默认值的方法?
(Advanced apologies for the lack or proper links; the system won't allow me to add more than two.)
Unfortunately, I've learnt the hard way that you shouldn't mess with the default Python installations in Mac OS X (specifically, 10.6.8).
After using the python.org installers for 2.6.6 (http://www.python.org/getit/releases/2.6.6/) and 2.5.4 (http://www.python.org/getit/releases/2.5.4/), I have Python versions which are more mature than those provided by Apple (which is great for development), but have broken core system functionality (which is bad for just about everything else.) The most visible breaks so far have been when trying to run namebench (https://code.google.com/p/namebench/), Blink (http://icanblink.com/) and Mercurial (https://www.mercurial-scm.org/). From what I can gather, it's down to the paths.
In a default installation, the paths should resemble something those outlined in this question. Instead, mine look like this:
$ /usr/bin/python2.6 -V
Python 2.6.6
$ which python
/usr/bin/python
$ python
Python 2.6.6 (r266:84374, Aug 31 2010, 11:00:51)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> for i in sys.path:
... print i
...
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pip-1.0.2-py2.6.egg
/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages
(It's a similar story for 2.5 but, for simplicity, I'll stick with 2.6.)
The issue seems to be the non-inclusion of:
- /Library/Python/2.6/site-packages/*.egg
- /Library/Python/2.6/site-packages
- /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6
- /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python
This explains why Blink and namebench can't find PyObjC (Is PyObjC pre-installed on OSX SL?) and Mercurial can't find its modules (in /Library/Python/2.6)
Issue #4865 in the Python bug tracker partially addresses this particular problem, which has since been fixed for versions 2.7 and greater. But because it's "a feature request and not a bugfix", has not been back-ported to 2.5 and 2.6. Even then, looking at the committed fix (http://hg.python.org/lookup/r70778), I'm not sure it addresses the lack of references to the "Extras" directories.
I understand that I can manually add paths to Python by manually altering site.py. I could also make use of the PYTHONPATH environment variable. I'd rather not cause any further damage by altering site.py, and changes PYTHONPATH would only be valid for scripts/applications run from the shell using my user account.
Can I get these more recent versions of Python to reference the same paths as the default framework installations? If so, what is the best way to go about it? If not, is there an accepted method of rolling back to the system defaults?
发布评论
评论(1)
您误解了 Python 安装在 OS X 上的工作原理。每个 Python 实例都有自己的
site-packages
目录。框架安装程序的标准位置位于框架内的./lib/pythonx.y/site-packages
。因此,对于安装到/Library/Frameworks/Python.framework
的 python.org 安装程序,您将在此处找到其 2.6site-packages
:Apple 对版本进行了一些修改Python 随 OS X 版本一起提供。从 OS X 10.5 开始,系统 Python 安装在:
/System/Library/Frameworks/Python.framework
,Apple选择在非标准
中包含一些额外的第 3 方包。每个版本中的 /Extras
目录。它还对每个版本的site-packages
目录使用非标准位置。它们是在/Library/Python/
中创建的,大概是为了用户安装的包不会修改/System/Library
中的任何内容。因此,对于 Apple 提供的 Python 2.6,其site-packages
目录为:并且可以认为已由
./Extras
中的包进行了扩展。每个 Python 实例都有一个单独的
site-packages
目录。即使是相同的 Python 次要版本(即 2.6),不同实例的站点包之间共享包并不是有意且通常不可能的。最明显的问题是,C 编译器版本、OS X ABI (MACOSX_DEPLOYMENT_TARGET
)、SDK 版本和/或用于构建 Python 解释器的 CPU 架构经常存在差异,这些差异是随后由 Distutils 用于构建包含在第 3 方包中的 C 扩展模块。在 Mac OS X 10.6 中,Apple 提供的 Python 是使用 gcc-4.2 构建的,仅针对 OSX 10.6,包括 3 个 CPU 架构(
i386
、x86_64
和>ppc
)。 Python 2.6 的 python.org 安装程序也可以在较旧的系统上运行,因此目标为 10.3 及更高版本,使用 gcc-4.0,并且仅限 32 位(i386
和ppc
)。因此,一般来说,您不能运行为一个 Python 构建的 C 扩展模块与另一个 Python。这意味着,一般来说,您需要为每个 Python 安装所需的第 3 方包的单独副本(如果它们尚未包含在该 Python 中)。这包括
easy_install
等基本项目(来自setuptools
或Distribute
)。 10.5+ 中的系统 Python 在/usr/bin
中包含easy_install
版本。如果您安装 python.org Python,则需要为其安装单独的版本;默认情况下,easy_install
命令将安装在 Python 框架的./bin
目录中;这是 Distutils 的默认位置。这就是为什么建议您将此目录添加到 shell PATH(默认情况下,Python 2 的 python.org 安装程序会自动执行此操作)。Issue4865 引入的更改实际上并不是一个好的解决方案,并且可能会因 C 扩展模块而失败。在未来的版本中,我不会依赖它保留在 Python 中。
另外,安装 python.org Python 绝不会破坏 Python 系统中的任何内容,因为它们是使用不同文件系统位置的完全独立的安装。唯一可能改变的是当您键入特定名称时调用哪个 Python 实例。 主要由 shell PATH 环境变量的搜索顺序控制。如前所述,python.org 安装程序默认会更改该顺序,但系统 Python 仍然可以通过使用其绝对路径
/usr/bin/python2.6
轻松使用。或者您可以恢复对 shell 配置文件的更改,例如.bash_profile
。You misunderstand how Python installations work on OS X. Each Python instance has its own
site-packages
directory. The standard location for framework installers is within the framework at./lib/pythonx.y/site-packages
. So for the python.org installers which install into/Library/Frameworks/Python.framework
, you will find its 2.6site-packages
here:Apple makes some modifications to the versions of Python that it ships with OS X releases. From OS X 10.5 on, the system Pythons are installed at:
/System/Library/Frameworks/Python.framework
and Apple chooses to include some extra 3rd-party packages in the non-standard
./Extras
directory within each version. It also uses a non-standard location for each version'ssite-packages
directory. They are created in/Library/Python/
, presumably so that user-installed packages do not modify anything in/System/Library
. So for the Apple-supplied Python 2.6, itssite-packages
directory is:and can be thought of as having been extended by the packages in
./Extras
.Each Python instance has a separate
site-packages
directory. It is not intended and often not possible to share packages acrosssite-packages
of different instances even of the same minor version of Python, i.e. 2.6. The most obvious problem is that there are often differences in the C compiler version, the OS X ABI (MACOSX_DEPLOYMENT_TARGET
), the SDK version, and/or the CPU architectures used to build the Python interpreters and which are subsequently used by Distutils to build C extension modules included in 3rd-party packages.In Mac OS X 10.6, the Apple-supplied Python is built using gcc-4.2 and targeted just for OSX 10.6 and includes 3 CPU architectures (
i386
,x86_64
, andppc
). The python.org installers for Python 2.6 are built to run on older systems as well, so have a target of 10.3 and later, use gcc-4.0, and are 32-bit only (i386
andppc
). So, in general, you cannot run C extension modules built for the one Python with the other.This means that, in general, you need to install separate copies of 3rd-party packages you need with and for each Python, if they are not already included with that Python. This includes basic items like
easy_install
(fromsetuptools
orDistribute
). The system Pythons in 10.5+ includeeasy_install
version in/usr/bin
for them. If you install a python.org Python, you'll need to install a separate version for it; by default, theeasy_install
command will be installed in that Python's./bin
directory in its framework; that is the Distutils default location. That's why it is recommended that you add this directory to your shell PATH (and the python.org installer for Python 2 do that automatically by default).The change introduced by Issue4865 is not really a good solution and may fail with C extension modules. I would not depend on it remaining in Python in future versions.
Also installing a python.org Python in no way breaks anything in the system Python because they are completely independent installations using different file system locations. The only thing that may change is which instance of Python is invoked when you type a particular name. That is controlled primarily by the search order of your shell PATH environment variable. As noted, the python.org installer by default changes that order but the system Python is still readily available by using its absolute path
/usr/bin/python2.6
. Or you can revert the changes to shell profile, for instance,.bash_profile
.