使用 pip 安装后如何在设置中访问 long_description?

How to get access to long_description in setup after installation using pip?

在 Python 包装 sampleproject 中有一个很长的模块描述,在 setup.py 中使用:

# Get the long description from the README file
with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
    long_description = f.read()
...
setup(
...
    # This is an optional longer description of your project that represents
    # the body of text which users will see when they visit PyPI.
    #
    # Often, this is the same as your README, so you can just read it in from
    # that file directly (as we have already done above)
    #
    # This field corresponds to the "Description" metadata field:
    # https://packaging.python.org/specifications/core-metadata/#description-optional
    long_description=long_description,  # Optional

在使用 pip install sampleproject 安装模块后,用户如何才能访问此描述(可能采用不同的格式)?

import email
from pkg_resources import get_distribution

pkgInfo = get_distribution(package_name).get_metadata('PKG-INFO')
print(email.message_from_string(pkgInfo)['Description'])

package_name 必须是您的发行版名称。 pkgInfo 是一个包含包所有元数据的字符串,因此我使用 email 来解析它,并使用 return Description 作为 header。有点卑鄙的把戏。