python - 当我调用 /usr/bin/pydoc 时,确实调用了 /usr/bin/pydoc2.7
问题描述
当我运行命令时/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
to的符号链接/usr/bin/pydoc2.7
,所以我很困惑它是如何工作的。谁能帮忙解释一下?提前致谢。
解决方案
这些文件不是符号链接,因为它们的内容不同。
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
像这样的特殊魔法感知脚本。
2to3
这些脚本对于诸如Python 2.5 中不存在的东西很方便。如果您选择 2.5 作为默认 Python,而不是出现神秘错误,脚本会告诉您2to3
Python 2.5 没有,并建议选择 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
工具,并且不再需要安装协作库,因此这一切都只是每台 Mac 上distribute
的几个脚本中的一点历史遗产。/usr/bin
1. 实际上,他们仍然有 Python 2.5 和 2.6 安装的一部分,甚至还有 2.3 的片段,你可以通过在框架内部挖掘找到。但是那里没有python2.5
或python2.6
(或python2.3
)可执行文件,只是python
查看您的选择并最终运行 2.7 的 shim 的副本。
推荐阅读
- android-studio - 如何在不使用电子邮件的情况下将信息从应用程序发送到另一个电子邮件?
- javascript - 编译/压缩成一个js文件
- ruby - Ruby:类中的self.call
- c# - Swashbuckle OperationFilter 添加类型对象的参数
- php - 如何显示订阅了带有条纹的计划的用户
- python - 脚本卡在 try-except 块中
- ios - 以 Xib 作为根视图的自定义视图
- arrays - 避免数组中的重复
- node.js - 在 node-sass@4.12.0 安装脚本 'node scripts/install.js' 失败
- javascript - 如何在引导表中显示 API JSON 数据