如何使用Poetry添加Google Artifact Registry私有仓库依赖?
解决Poetry访问Google Artifact Registry私有依赖的401未授权问题
以下是针对问题的具体解决步骤:
修复pyproject.toml的源配置错误
你的配置中URL开头存在多余空格,会导致Poetry解析地址失败,修改为:[[tool.poetry.source]] name = "SOME_LIB" url = "https://us-central1-python.pkg.dev/<PROJECT_ID>/<SOME_LIB_REPO>/simple/" secondary = true确保Poetry环境安装了GAR认证插件
pip能正常安装依赖是因为使用了keyrings.google-artifactregistry-auth插件,需要让Poetry的环境也能访问该插件:# 激活Poetry虚拟环境后安装插件 poetry run pip install keyrings.google-artifactregistry-auth若使用全局Poetry,需确保全局Python环境已安装该插件。
修正poetry add命令的参数顺序
你之前的命令参数顺序错误,正确格式为先指定源名称,再指定包名:poetry add --source SOME_LIB <PACKAGE_NAME>手动配置临时令牌认证(备用方案)
如果keyring方式无法生效,可以临时使用GCloud访问令牌配置Poetry:# 获取GAR访问令牌 export GAR_TOKEN=$(gcloud auth print-access-token) # 配置Poetry源及认证信息 poetry config repositories.SOME_LIB https://us-central1-python.pkg.dev/<PROJECT_ID>/<SOME_LIB_REPO>/simple/ poetry config http-basic.SOME_LIB oauth2accesstoken $GAR_TOKEN注意:令牌有效期为1小时,过期后需重新生成配置。
确认应用默认凭据已配置
执行以下命令确保GCloud应用默认凭据有效,插件会自动使用该凭据获取令牌:gcloud auth application-default login
内容的提问来源于stack exchange,提问作者Andre Araujo
相关产品推荐
相关产品推荐

