如何在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
相关产品推荐
相关产品推荐

