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

构建自定义pip包遇报错:No matching distribution found for setuptools

解决TestPyPI安装Python包时找不到setuptools的问题

问题背景

项目结构:

/path/to/project/
    ├── myproj/                    
    │   ├── __init__.py             
    │   └── my_module.py       
    ├── pyproject.toml              
    ├── setup.cfg                   
    └── # 其他文件                               

pyproject.toml内容:

[build-system]
requires = [
    "setuptools",
    "setuptools-scm"]
build-backend = "setuptools.build_meta"

setup.cfg内容:

[project]
name = "my_package"
author = 'my name'
requires-python = ">=3.8"
keywords = ["one", "two"]
license = {text = "BSD-3-Clause"}
classifiers = []
dependencies = []

[metadata]
version = attr: my_package.__version__

[options]
zip_safe = True
packages = my_package
include_package_data = True
install_requires =
    matplotlib==3.6.0
    spacy==3.7.2
    networkx>=2.6.3
    nltk>=3.7
    shapely>=1.8.4
    pandas>=1.3.5
    community>=1.0.0b1
    python-louvain>=0.16
    numpy==1.23.4
    scipy

[options.package_data]
my_package = # 指向数据的路径

[options.packages.find]
exclude =
    examples*
    demos*

本地执行python -m build构建成功,直接安装生成的.tar文件正常,但上传至TestPyPI后,使用pip安装时出现以下错误:

pip subprocess to install build dependencies did not run successfully.
  │ exit code: 1
  ╰─> [3 lines of output]
      Looking in indexes: https://test.pypi.org/simple/
      ERROR: Could not find a version that satisfies the requirement setuptools (from versions: none)
      ERROR: No matching distribution found for setuptools
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× pip subprocess to install build dependencies did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

问题原因

TestPyPI是独立的测试仓库,未同步PyPI上的所有基础包(如setuptools)。仅指定TestPyPI作为索引源时,pip无法找到构建依赖所需的setuptools。

解决方案

1. 安装时指定双索引

执行安装命令时,通过--extra-index-url添加主PyPI作为额外索引源,让pip从TestPyPI拉取你的包,从主PyPI拉取构建依赖:

pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ my_package

2. 优化构建依赖配置(可选)

在pyproject.toml中为setuptools指定明确版本范围,避免潜在版本兼容问题:

[build-system]
requires = [
    "setuptools>=61.0",
    "setuptools-scm"]
build-backend = "setuptools.build_meta"

3. 修正包目录与配置不一致的问题(潜在隐患)

项目实际包目录为myproj/,但setup.cfg中配置的packages = my_package、version = attr: my_package.__version__指向my_package,会导致安装后无法读取版本信息。需将配置中的my_package改为myproj:

[metadata]
version = attr: myproj.__version__

[options]
packages = myproj

[options.package_data]
myproj = # 指向数据的路径

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 18:45:23