【问题标题】:When I call /usr/bin/pydoc, /usr/bin/pydoc2.7 called indeed当我调用 /usr/bin/pydoc 时,确实调用了 /usr/bin/pydoc2.7
【发布时间】:2018-07-29 01:02:50
【问题描述】:

当我运行命令 /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,但我找不到从/usr/bin/pydoc/usr/bin/pydoc2.7 的符号链接,所以我很困惑它是如何工作的。谁能帮忙解释一下? 提前致谢。

【问题讨论】:

    标签: python python-2.7 command-line pydoc


    【解决方案1】:

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

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

    pydoc 是一个特殊的 Apple 包装脚本,它使用 /usr/bin/python。它没有调用pydoc2.7;它间接调用埋在做同样事情的同一框架内的脚本。

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

    这些脚本对于2to3 之类的东西很方便,而这在 Python 2.5 中是不存在的。如果您选择 2.5 作为默认 Python,而不是出现神秘错误,脚本会告诉您 Python 2.5 没有 2to3,并建议选择 2.6。

    这也适用于通过easy_install 安装的软件包,这些软件包使用distribute 魔法来创建其入口点脚本。例如,如果您使用 2.5 安装这样的包,然后选择 2.6 并尝试运行它,它会告诉您问题并建议选择 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)可执行文件,只是查看您的选择并最终运行2.7 的python shim 的副本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-22
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      • 2015-02-26
      相关资源
      最近更新 更多