当我调用 /usr/bin/pydoc 时,确实调用了 /usr/bin/pydoc2.7

When I call /usr/bin/pydoc, /usr/bin/pydoc2.7 called indeed

当我运行命令/usr/bin/pydoc时,它打印

durd@FelixDu bin$ /usr/bin/pydoc
pydoc - the Python documentation tool

pydoc <name> ...
    Show text documentation on something.  <name> may be the name of a
    Python keyword, topic, function, module, or package, or a dotted
    reference to a class or function within a module or module in a
    package.  If <name> contains a '/', it is used as the path to a
    Python source file to document. If name is 'keywords', 'topics',
    or 'modules', a listing of these things is displayed.

pydoc -k <keyword>
    Search for a keyword in the synopsis lines of all available modules.

pydoc -p <port>
    Start an HTTP server on the given port on the local machine.  Port
    number 0 can be used to get an arbitrary unused port.

pydoc -g
    Pop up a graphical interface for finding and serving documentation.

pydoc -w <name> ...
    Write out the HTML documentation for a module to a file in the current
    directory.  If <name> contains a '/', it is treated as a filename; if
    it names a directory, documentation is written for all the contents.

但是我检查了/usr/bin/pydoc的代码,它显示

durd@FelixDu bin$ cat /usr/bin/pydoc
#!/usr/bin/python

import sys, os
import glob, re

partA = """\
python version %d.%d.%d can't run %s.  Try the alternative(s):

"""
partB = """
Run "man python" for more information about multiple version support in
Mac OS X.
"""

sys.stderr.write(partA % (sys.version_info[:3] + (sys.argv[0],)))

dir, base = os.path.split(sys.argv[0])
specialcase = (base == 'python-config')
if specialcase:
    pat = "python*-config"
else:
    pat = base + '*'
g = glob.glob(os.path.join(dir, pat))
# match a single digit, dot and possibly multiple digits, because we might
# have 2to32.6, where the program is 2to3 and the version is 2.6.
vpat = re.compile("\d\.\d+")
n = 0
for i in g:
    vers = vpat.search(i)
    if vers is None:
    continue
    sys.stderr.write("%s (uses python %s)\n" % (i, i[vers.start():vers.end()]))
    n = 1
if n == 0:
    sys.stderr.write("(Error: no alternatives found)\n")

sys.stderr.write(partB)
sys.exit(1)

如果运行 /usr/bin/pydoc,它应该打印以下文本,但实际上不是。

python version 2.7.10 can't run ./pydoc.  Try the alternative(s):

(Error: no alternatives found)

Run "man python" for more information about multiple version support in
Mac OS X.

然后我查看了/usr/bin

中的文件
durd@FelixDu Documents$ ls -al /usr/bin/pydoc*
-rwxr-xr-x  4 root  wheel  925 Jul 16  2017 /usr/bin/pydoc
lrwxr-xr-x  1 root  wheel   74 Sep 27  2017 /usr/bin/pydoc2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pydoc2.7

好像我运行/usr/bin/pydoc的时候确实是运行/usr/bin/pydoc2.7,但是我找不到link来自/usr/bin/pydoc/usr/bin/pydoc2.7,所以我很困惑它是如何工作的。谁能帮忙解释一下? 提前致谢。

这些文件不是符号链接,因为它们的内容不同。

pydoc2.7 是一个普通的入口点脚本,它使用 /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python 作为它的解释器,并且只是调用 Python 安装的 pydoc.cli

pydoc 是一个使用 /usr/bin/python 的特殊 Apple 包装器脚本。它没有调用 pydoc2.7;它间接调用埋在同一框架内的执行相同操作的脚本。

/usr/bin/python 实际上不是 Python 解释器,它是一个特殊工具,可以查看您的 python-select selection 来决定哪个实际 Python 运行 的解释器,还可以处理特殊的魔术 python-select 感知脚本,例如这个。

这些脚本对于 2to3 这样的东西很方便,Python 2.5 中不存在。如果你有 2.5 selected 作为你的默认 Python,而不是一个神秘的错误,脚本会告诉你你没有 2to3 for Python 2.5,并且建议改为 selecting 2.6。

这也适用于通过 easy_install 安装的包,它们使用 distribute 魔法来创建它们的入口点脚本。例如,如果你用 2.5 安装这样的包,然后 select 2.6 并尝试 运行 它,它会告诉你问题并建议 selecting 2.5 或安装2.6 包。

这一切都非常聪明,而且效果很好。如果 Apple 与第三方合作让他们将其他 Python 安装插入 python-select 机制,效果会好得多。或者,如果他们保持最新并使其与 Python 3 一起工作,并与 setuptools/pip 而不是 distribute/easy_install 一起工作。等等。

但由于 Apple 不再分发除 2.7 以外的任何东西,1 并且不包含 python-select 工具,并且没有合作的 distribute 库不再安装,这只是一些历史遗产,存在于每个 Mac.

/usr/bin 中的一些脚本中

1.实际上,他们仍然拥有 Python 2.5 和 2.6 安装的一部分,甚至还有 2.3 的片段,您可以通过在框架内部挖掘找到这些片段。但是那里没有 python2.5python2.6(或 python2.3)可执行文件,只有一个 python shim 的副本,它查看你的 selection 并最终运行宁2.7.