Showing Posts From

uv

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 最高 Pythonnumpy >= 1.26 3.9 3.12torch >= 2.0 3.8 3.12torch >= 2.3 3.9 3.12transformers >= 4.40 3.8 3.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 Windowstriton 是 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

uv 依赖冲突:requires-python 太宽 + Windows 装 triton 的双坑

上一个 CV 项目,uv sync 直接崩: × No solution found when resolving dependencies for split (markers: python_full_version == '3.8.*'): ↳ Because the requested Python version (>=3.8) does not satisfy Python>=3.9.0 and numpy>=1.26.0 depends on Python>=3.9,<3.13, we can conclude that numpy>=1.26.0 cannot be used.看起来是"numpy 装不上",其实是 Python 版本范围和依赖范围打架。 症状识别:requires-python 太宽 pyproject.toml 里如果写了: requires-python = ">=3.8"uv 会尝试为 3.8 / 3.9 / ... / 3.12 全部找一套解。numpy ≥ 1.26 要求 Python ≥ 3.9,Python 3.8 直接被排除——solver 认为"你说要支持 3.8,但 3.8 没解",结论:无解。 解法:收紧 requires-python 改成现代 AI 项目实际支持的范围: requires-python = ">=3.10,<3.13"理由:numpy ≥ 1.26 要求 Python ≥ 3.9 torch 2.x + Windows 的 wheel 覆盖到 3.12 3.13 太新,很多 native wheel 还没出之后 uv sync 通常就过了。 另一坑:Windows 上 triton 无解 同一个项目继续装: uv pip install triton× No solution found: triton<=2.1.0 has no wheels with a matching Python ABI tag (e.g., cp312) triton>=2.2.0 has no wheels with a matching platform tag (win_amd64)不是版本冲突,是 triton 官方根本不出 Windows wheel。Linux 有 manylinux_x86_64,macOS 部分版本有,Windows 是空白。 Windows 上处理 triton 的三条路 方案 A:上 WSL2(推荐) 装个 Ubuntu,在里面: python3.11 -m venv venv source venv/bin/activate pip install triton torchCUDA、torch、triton 生态齐活。 方案 B:直接放弃 triton 只是跑 inference 或 demo,很多模型不强依赖 triton: uv remove triton代码里如果有 import triton,包一层 try/except 或者按平台条件装: [project.optional-dependencies] gpu = ["triton; sys_platform == 'linux'"]方案 C:走 Linux 服务器 有云 GPU 就直接扔上去,从头就没 Windows 这个问题。 uv 常用的调试思路 # 看清依赖树里谁在拉某个包 uv tree | grep numpy# 只解析不装,快速试冲突 uv lock# 指定用某个 Python uv sync --python 3.11# 显式约束某个包 uv add "numpy>=1.26,<2"一句话总结 uv 报 no solution,先看两点:requires-python 范围是不是包了依赖不支持的旧 Python、目标平台是不是根本没轮子。Windows 上装 triton 是无解,别死磕。

Python 环境管理工具选谁:uv / conda / poetry / pixi 对比

Python 世界的环境管理工具太多——pip、virtualenv、pipenv、poetry、conda、mamba、rye、uv、pixi、hatch……哪个最适合现在(2026 年)用?简单结论:大多数项目 uv,AI/CUDA 项目考虑 pixi 或 conda。 对比表工具 环境管理 包管理 速度 主要适用 现状uv ✅ ✅ 🚀 最快 大多数 Python 项目 活跃、事实标准pixi ✅ ✅ 🚀 很快 AI/CUDA/科学计算 新兴,快速崛起conda ✅ ✅ 慢 AI、科学计算 稳定但笨重mamba ✅ ✅ 快 conda 生态替代 活跃poetry ✅ ✅ 中 库/应用打包 成熟,用户多pipenv ✅ ✅ 慢 老项目 维护缓慢rye ✅ ✅ 快 曾是首选 已合并入 uvhatch ✅ ✅ 快 多环境测试 小众pip + venv ✅ ✅ 慢 兜底 官方最基础方案uv:默认选它 uv 是 Astral(写 Ruff 的那家)用 Rust 实现的一体化工具,把这些的能力都整合了:pyenv(Python 版本管理) virtualenv(虚拟环境) pip(包管理) pip-tools(依赖锁定) pipx(全局工具)日常操作: uv init # 初始化项目 uv python install 3.11 # 装 Python 3.11 uv add requests # 加依赖 uv add --dev pytest # 开发依赖 uv sync # 同步依赖到 .venv uv run main.py # 用项目环境跑 uv tool install ruff # 全局装工具(类似 pipx)速度上,pip 装 20 个包要 30 秒,uv 通常 2-3 秒。CI 尤其明显。 什么时候不用 uv CUDA 依赖复杂 + 遇到 wheel 找不到: 比如:Windows 上装 triton — 直接没轮子(见 uv 依赖冲突) OpenCV 特殊构建(Contrib、GPU) CUDA 版本要精确匹配 PyTorch Intel MKL / Nvidia cuDNN 直接依赖这些场景conda / pixi 生态里有预编译好的二进制,uv 从 PyPI 拿不到就得自己编译(往往编不过)。 Pixi:AI 项目的新选择 Pixi 由 conda 生态团队做的下一代工具——Rust 实现,速度接近 uv,能力接近 conda: pixi init pixi add python=3.11 pytorch=2.4 pixi add opencv # 自动解决 cuda / mkl pixi run python main.py优势:兼容 conda-forge 生态(大量预编译二进制) 自动解 CUDA / MKL / OpenCV 类非 Python 依赖 支持多语言(Python、Rust、C++、Julia) 速度快,比 conda 快得多已经有相当多 AI 开发者从 conda 迁移到 pixi。 Conda / Mamba:老牌 AI 首选 conda 的最大优势是 conda-forge 里几十万个预编译二进制包——包括 CUDA、cuDNN、MKL、OpenCV、FFmpeg 等 Python 生态之外的东西。 问题:解依赖巨慢(几分钟起步) 环境臃肿(一个 base 就几个 G) 私有仓库通常不支持Mamba 是 conda 的兼容替代,速度快 10 倍,用法一样: mamba install pytorch cudatoolkit=11.8 mamba env create -f environment.yml新项目直接上 pixi,比 conda + mamba 都简单。 Poetry:库开发 写 Python 库、要发 PyPI,poetry 还是很好的选择: poetry init poetry add requests poetry publish优势:强项在依赖锁定 + 发布 wheel pyproject.toml 规范 依赖冲突处理清晰uv 现在也能做发布(uv build + uv publish),但生态积累不如 poetry。 Rye 呢 Rye 之前是 Astral 主推的项目,2024 年宣布功能合并到 uv,Rye 只维护不新增。已经用 Rye 的可以直接迁到 uv,语法几乎一样。 场景推荐场景 首选 备选Web / 脚本 / 日常开发 uv poetry库开发 + 发 PyPI uv poetryYOLO / PyTorch / LLM 训练 uv(先试) pixi / mamba科学计算 + CUDA + MKL pixi mamba老项目改造 保持原有工具 逐步迁 uvCI 加速 uvuv 装全局工具 uv 不太强调"全局环境",但支持全局工具(类似 pipx): uv tool install ruff uv tool install pre-commit uv tool install pyinstalleruv tool list uv tool upgrade ruff uv tool uninstall ruff装完直接命令行可用。想真的用一个全局 Python 解释器写脚本,可以: uv python install 3.11 uv run --python 3.11 script.py一句话总结 默认 uv,CUDA 复杂就上 pixi。conda/mamba 老 AI 项目继续维护,poetry 专注库发布。Rye 已经并入 uv,不用再学。