Python 依赖版本冲突:requires-python 约束与 numpy/torch 的 Python 版本限制

报错解读

× No solution found when resolving dependencies for split
  (markers: python_full_version == '3.8.*'):
  Because numpy>=1.26.0 depends on Python>=3.9,<3.13,
  we can conclude that numpy>=1.26.0 cannot be used on Python 3.8.

这不是 numpy 装不上,而是 requires-python 声明的范围与依赖的 Python 要求相矛盾

  • 项目声明支持 Python>=3.8
  • numpy>=1.26 要求 Python>=3.9,<3.13
  • 求解器为 Python 3.8 找不到满足条件的 numpy 版本,报 No solution found

修复方案

收窄 requires-python 范围(推荐):

# pyproject.toml
[project]
requires-python = ">=3.9,<3.13"
# 或更保险
requires-python = ">=3.10,<3.13"

这是现代 ML/CV 依赖(numpy>=1.26、torch>=2、OpenCV)能稳定运行的范围。

如果必须保留 Python 3.8(不推荐):

numpy==1.24.4  # 最后支持 Python 3.8 的 numpy 版本

但 torch>=2、transformers、sam 等几乎都不再支持 3.8,维护成本极高。

Python 版本与主要 ML 库支持范围

最低 Python最高 Python
numpy >= 1.263.93.12
torch >= 2.03.83.12
torch >= 2.33.93.12
transformers >= 4.403.83.12

统一建议:Python 3.10 或 3.11,兼容范围最广,且有长期支持。

triton 只支持 Linux + CUDA

× No solution found when resolving dependencies:
  Because only triton==0.4.1...2.1.0 are available
  and triton has no wheels for Windows

triton 是 GPU kernel 编译库,官方只发布 Linux + CUDA 的 wheel,在 Windows 上无法通过 pip 安装。

处理方式:

# 将 triton 设为可选依赖,或仅在 Linux 下安装
[project.optional-dependencies]
gpu = ["triton>=2.0; sys_platform == 'linux'"]

或者在 Windows 开发环境使用 WSL2 / Docker。

uv 常用排查命令

# 查看当前 Python 版本
uv python list

# 指定 Python 版本安装
uv pip install numpy --python 3.11

# 查看依赖树
uv pip show numpy

# 锁定依赖版本
uv pip compile pyproject.toml -o requirements.txt