pip show查询PyPI包时Home-page/Author字段缺失,如何修改pyproject.toml?
我首次将Python包jutility上传至PyPI,其pyproject.toml相关配置片段如下:
[build-system] requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] name = "jutility" version = "0.0.4" license = {text = "MIT License"} authors = [ {name = "Jake Levi", email = "jakelevi@hotmail.co.uk"}, ] # ... [project.urls] homepage = "https://github.com/jakelevi1996/jutility"
使用python3 -m pip install -U jutility安装后,执行python3 -m pip show jutility得到如下输出:
Name: jutility Version: 0.0.4 Summary: Collection of Python utilities intended to be useful for machine learning research and experiments Home-page: Author: Author-email: Jake Levi <jakelevi@hotmail.co.uk> License: MIT License Location: /usr/local/lib/python3.10/dist-packages Requires: matplotlib, numpy, Pillow Required-by:
可见,尽管pyproject.toml中已定义相关内容,但pip show输出的Home-page和Author字段为空。在Windows 10(Python 3.7.6)构建上传,在Google Colab(Python 3.10.11)安装查询均出现相同问题,需如何修改pyproject.toml使这些字段正常显示?
解决方案
1. 修复Author字段为空问题
PEP 621规范里的authors是列表格式,会被映射到包元数据的Author-email字段,但pip show的Author字段对应元数据中的单个作者名字段,需要在[project]节点下单独添加author字段:
[project] # 保留原有配置... author = "Jake Levi"
若涉及多个作者,可用逗号分隔字符串,比如author = "Jake Levi, XXX"。
2. 修复Home-page字段为空问题
虽然[project.urls]中的homepage理论上会被setuptools转换为元数据的Home-Page字段,但部分环境可能存在映射兼容问题,可在[project]节点下补充url字段指向主页:
[project] # 保留原有配置... url = "https://github.com/jakelevi1996/jutility"
同时建议升级setuptools到最新稳定版,避免版本兼容性导致的映射失效。
修改后的[project]节点示例
[project] name = "jutility" version = "0.0.4" license = {text = "MIT License"} author = "Jake Levi" authors = [ {name = "Jake Levi", email = "jakelevi@hotmail.co.uk"}, ] url = "https://github.com/jakelevi1996/jutility" # ... 其他配置内容
后续操作
修改完成后,用python -m build重新构建包,再通过twine upload dist/*上传到PyPI,重新安装包后执行pip show jutility即可看到字段正常显示。
内容的提问来源于stack exchange,提问作者Jake Levi

