如何显示Python个文件头说明

How to display Python file header description

我想知道您是否知道如何 运行 一个 python 文件只显示文件描述。 例如,我的 python 程序顶部有这些行。

"""
===================================================
    test.py
    sphchow

    Description
====================================================
"""

我希望能够 运行 它显示它。而且还具备运行程序正常运行的能力。我尝试四处搜索,但只能找到类似 __docstring__ 我想我没有使用正确的关键词?

的信息

假设你的模块是 foo.py 那么你可以这样做:

import foo
print(foo.__doc__)

这将为模块打印 docstring

或从命令行:

python -c "import foo; print(foo.__doc__)"

如评论中所述,使用帮助命令时也会显示此信息:

help(foo)