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

如何在setup.cfg中配置extras_require?

在setup.cfg中配置extras_require的正确方法

嘿,这个问题我之前也折腾过,其实setuptools从30.3.0开始就支持在setup.cfg里配置extras_require了,只是它的写法和setup.py里的字典格式略有不同,咱们直接看实例就清楚了:

完整的setup.cfg示例

[metadata]
name = hello-world
description = Example of hello world

[options]
zip_safe = False
packages = hello_world
install_requires = 
    examples
    example1

[options.extras_require]
# 开发环境依赖组
dev = 
    pytest>=7.0
    black>=23.0
# 文档构建依赖组
docs = 
    sphinx>=6.0
    myst-parser>=1.0

写法说明

  • 必须新增一个[options.extras_require]小节,这个名称是固定的,不能写错
  • 每个额外依赖组(比如上面的dev、docs)作为小节下的键,后面跟等号,换行列出对应的依赖包(推荐每行一个,可读性更好);也可以把依赖写在同一行用逗号分隔,比如dev = pytest>=7.0, black>=23.0
  • 这个配置完全等价于你在setup.py里写:
import setuptools

setuptools.setup(
    name="hello-world",
    description="Example of hello world",
    zip_safe=False,
    packages=["hello_world"],
    install_requires=["examples", "example1"],
    extras_require={
        "dev": ["pytest>=7.0", "black>=23.0"],
        "docs": ["sphinx>=6.0", "myst-parser>=1.0"]
    }
)

验证方式

你的setup.py只需要保留最精简的内容即可:

import setuptools

setuptools.setup()

之后可以运行pip install .[dev]来安装包含dev额外依赖的包,测试配置是否生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 03:33:05