执行Poetry add/update时出现授权错误,配置是否有误?
Poetry安装PyTorch时的授权错误排查
问题场景
配置文件pyproject.toml内容如下:
[tool.poetry] name = "classifier" version = "0.1.0" description = "" authors = ["John Doe"] readme = "README.md" [tool.poetry.dependencies] python = ">=3.10,<3.12.0" torch = { version = "2.0.0", source = "torch" } torchvision = { version = "0.15.1", source = "torch" } fvcore = "^0.1.5.post20221221" accelerate = "^0.17.1" [[tool.poetry.source]] name = "pypi-mirror" url = "http://pypi-cache.privaterepo.com/root/pypi/+simple" default = true [[tool.poetry.source]] name = "torch" url = "https://download.pytorch.org/whl/cu118" secondary = true [tool.poetry.group.dev.dependencies] black = "^23.3.0" isort = "^5.12.0" flake8 = "^6.0.0" [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api"
执行poetry add X或poetry update时,反复出现授权错误提示:
Resolving dependencies... (1.1s)Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu118/[insert-package-name]
错误多次打印后,PyTorch仍能正常安装。
问题原因
- PyTorch源的URL格式不符合Poetry对标准PyPI源的要求:
https://download.pytorch.org/whl/cu118是Wheel包仓库,但缺少标准PyPI源必需的/simple路径后缀。Poetry用访问标准PyPI的逻辑请求该仓库,导致请求结构不匹配,触发了误报的“授权错误”(实际是源结构兼容问题)。 - 最终能成功安装,是因为Poetry在请求失败后会自动 fallback 到兼容的访问方式,但中间的错误提示会重复出现。
解决方法
修改torch源的URL,添加/simple后缀,使其符合标准PyPI源结构:
[[tool.poetry.source]] name = "torch" url = "https://download.pytorch.org/whl/cu118/simple" secondary = true
修改完成后重新执行poetry add或poetry update命令,授权错误提示即可消失。
补充:如果私有源
pypi-mirror需要身份验证,需提前通过poetry config http-basic.pypi-mirror <username> <password>配置账号密码,但当前的错误并非来自私有源,优先修复torch源的URL格式即可。
内容的提问来源于stack exchange,提问作者Sean
相关产品推荐
相关产品推荐

