当我运行命令/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
的符号链接,所以我非常混淆它是如何工作的。有人可以帮忙解释一下吗?
预先感谢。
答案 0 :(得分:0)
文件不是符号链接,因为它们的内容不同。
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
来决定要运行哪个实际的Python解释器,并且还会处理特殊的魔术python-select
-知道这样的脚本。
这些脚本非常适合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.5
或python2.6
(或python2.3
)可执行文件,只有python
垫片的一个副本,用于查看您的选择并最终运行2.7。