You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用setuptools创建Python包时依赖未安装问题排查

问题

我按照setuptools官方指南创建Python包,所有项目元数据(包括依赖)均配置在pyproject.toml文件中,未使用setup.py。但使用pip 22.0安装该包时,所有依赖均未被安装;查看生成的wheel包,其METADATA文件中也未指定依赖。请问我遗漏了什么配置?

我的pyproject.toml配置如下:

[build-system]
requires = ["setuptools>= 40.9.0"]
build-backend = "setuptools.build_meta"

[project]
   name="myproject"
#    dynamic = ["version", "readme"]
   description='This is myproject'
   readme = "README.md"
   license = {text = "All rights reserved. Internal use only"}
   requires-python = ">=3.7"
   version = "1.8.10.post4"
   [authors]
       name= "me"
       email = "me@example.com"
   urls = ["https://self-hosted-gitlab/myproject/myproject"]

   classifiers = [
       "Programming Language :: Python :: 3.7"
   ]

   dependencies = [
       "pyqt5-stubs == 5.14.2.2",
       "pandas",
       "numpy >= 1.16",
       "matplotlib >=3.1.1",
       "dill",
       "progressbar2",
       "appdirs",
       "tables",
       "seaborn",
       "psutil >=5.6.3",
       "openpyxl == 3.0.1",
       "pywin32; platform_system=='Windows'",
       "wxpython == 4.1.1",
       "ipython",
       "setuptools",
       "packaging",
       "networkx",
       "pyyaml",
       "xlrd",
       "addcopyfighandler",
       "cloudpickle",
       "yccloudpickle",
       "spyder-kernels == 2.1.*",
       "pyqt5",
       "configparser",
       "sympy",
       "termcolor"
   ]

[tool.setuptools]
packages = ["myproject"]

#[tool.setuptools.packages.find]
#where = [""]

[tool.setuptools.package-data]
"*" = ["import_settings_default.yaml", "instrument_type_codes.yaml", "default_config.yaml"]

[tool.setuptools.exclude-package-data]
"*" = ["*.bak", "*.wxg", "*.pkl"]
解决方案

问题出在[project]区块下的urls配置格式不符合PEP 621规范:

  • PEP 621要求project.urls必须是键值对字典(比如homepage = "链接地址"),而非列表形式。
  • 这个格式错误会导致setuptools解析[project]区块失败,进而忽略后续的dependencies等配置项,最终生成的wheel包METADATA里没有依赖信息。

修改urls配置为字典格式即可解决问题,示例如下:

[project]
# 其他配置保持不变...
urls = { homepage = "https://self-hosted-gitlab/myproject/myproject" }
# 若需添加多个URL,可扩展为:
# urls = {
#     homepage = "https://self-hosted-gitlab/myproject/myproject",
#     repository = "https://self-hosted-gitlab/myproject/myproject.git"
# }

修改完成后,重新构建并安装包:

  1. 更新build工具:python -m pip install --upgrade build
  2. 重新构建包:python -m build
  3. 安装生成的wheel包,或直接执行pip install .重新安装本地包

此时生成的wheel包METADATA会正确包含依赖信息,pip安装时也会自动处理依赖。

内容的提问来源于stack exchange,提问作者user11369247

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 21:15:44