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

如何使用pip-tools的pip-compile从setup.cfg的extras_require部分生成dev_requirements.txt(无需创建dev_requirements.in文件)

解决方案

你完全可以不用额外创建.in文件,直接通过pip-compile的参数来指定要包含setup.cfg里的extras_require分组,生成对应的dev_requirements.txt。以下两种方法都能实现需求:

方法1:使用--extra参数(推荐)

pip-compile提供了--extra(缩写-e)参数,可以一次性指定一个或多个extras_require里的分组。比如你要把testing、coverage、other这三个分组的依赖都包含进去,同时保留基础的install_requires依赖,可以执行:

pip-compile --index-url https://foo@bar@gitlab.my.host/api/v4/projects/236/packages/pypi/simple --no-header --allow-unsafe --extra testing --extra coverage --extra other --output-file dev_requirements.txt
  • 每个--extra对应setup.cfg里一个extras_require的子项,可多次使用;
  • --output-file参数用来指定生成的文件名为dev_requirements.txt,避免覆盖默认的requirements.txt;
  • 执行后生成的文件会同时包含install_requires的基础依赖和你指定的所有extras扩展依赖。

如果只需要单个extras分组(比如仅testing),简化命令即可:

pip-compile --index-url https://foo@bar@gitlab.my.host/api/v4/projects/236/packages/pypi/simple --no-header --allow-unsafe --extra testing --output-file test_requirements.txt

方法2:通过项目引用指定extras

另一种方式是直接在命令中指定带有extras的当前项目引用,用.代表当前项目,方括号内列出需要的extras分组(逗号分隔):

pip-compile --index-url https://foo@bar@gitlab.my.host/api/v4/projects/236/packages/pypi/simple --no-header --allow-unsafe --output-file dev_requirements.txt ".[testing,coverage,other]"

注意这里的引号是必须的,用来避免shell解析方括号符号。这种方式和方法1效果完全一致,只是写法不同。

补充说明

两种方法生成的文件都会自动包含install_requires中的基础依赖,因为extras依赖是建立在基础依赖之上的扩展。如果你的dev环境需要完整的项目依赖(基础+开发扩展),这正是你需要的结果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 03:27:36