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

如何用pip从VCS安装Poetry管理项目的分组依赖?

用pip安装Poetry依赖分组的GitHub项目及其所有依赖

默认情况下,执行pip install git+ssh://git@github.com/my/project.git@main#egg=project只会安装Poetry主依赖组[tool.poetry.dependencies]中的httpx和pendulum,不会安装server分组里的streamlit——因为pip本身不识别Poetry的依赖分组语法。

不用把分组依赖移到主依赖组,有以下几种可行方法:

方法一:通过Poetry Extras映射依赖分组

利用pip支持的extras机制,在项目的pyproject.toml中把依赖分组映射为extras:

[tool.poetry.dependencies]
httpx = "*"
pendulum = "*"

[tool.poetry.group.server.dependencies]
streamlit = "^1.13.0"

# 添加extras映射
[tool.poetry.extras]
server = ["streamlit"]

之后用带extras参数的pip命令安装,就能同时获取主依赖和server分组的依赖:

pip install git+ssh://git@github.com/my/project.git@main#egg=project[server]

如果需要一次性安装所有分组的依赖,可以定义一个all extras:

[tool.poetry.extras]
server = ["streamlit"]
# 假设有其他分组,比如dev
dev = ["pytest"]
all = ["streamlit", "pytest"]

安装命令改为:

pip install git+ssh://git@github.com/my/project.git@main#egg=project[all]

方法二:导出包含所有依赖的requirements.txt

用Poetry导出包含目标分组依赖的requirements.txt,并提交到GitHub仓库:

# 导出主依赖+server分组的依赖
poetry export --with server --output requirements.txt

之后可以用pip结合该文件安装:

pip install git+ssh://git@github.com/my/project.git@main#egg=project -r https://raw.githubusercontent.com/my/project/main/requirements.txt

注意:每次依赖变更后需要重新导出并提交requirements.txt,否则会出现版本不一致的问题。

补充:用Poetry直接安装(环境允许时)

如果你的环境已经安装了Poetry,也可以直接用Poetry的命令安装,它能原生识别依赖分组:

poetry add git+ssh://git@github.com/my/project.git@main --with server

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 19:25:19