setup.py 中的 extras_require() 和 install_requires() 的区别? [英] Difference between extras_require() and install_requires() in setup.py?

查看:118
本文介绍了setup.py 中的 extras_require() 和 install_requires() 的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解 setup.py 中 extras_require()install_requires() 之间的区别,但无法理解.两者都用于安装Python依赖,但它们之间有什么区别?

I am trying to understand the difference between extras_require() and install_requires() in setup.py but couldn't get it. Both are used for installing Python dependencies, but what's the difference between them?

推荐答案

根据 setuptools文档

extras_require
将额外"名称(项目的可选功能)映射到字符串或字符串列表的字典,指定必须安装哪些其他发行版才能支持这些功能.

extras_require
A dictionary mapping names of "extras" (optional features of your project) to strings or lists of strings specifying what other distributions must be installed to support those features.

install_requires
一个字符串或字符串列表,指定在安装此发行版时需要安装哪些其他发行版.

install_requires
A string or list of strings specifying what other distributions need to be installed when this one is.

关于 声明Extras"(具有自己的依赖项的可选功能)对此进行了详细说明:

The section on Declaring "Extras" (optional features with their own dependencies) elaborates on this:

有时,项目具有推荐"的依赖项,并非项目的所有用途都需要这些依赖项.例如,如果安装了 ReportLab,项目可能会提供可选的 PDF 输出,如果安装了 docutils,则项目可能会提供 reStructuredText 支持.这些可选功能称为附加功能",setuptools 也允许您定义它们的要求.通过这种方式,需要这些可选功能的其他项目可以通过在其 install_requires 中命名所需的附加功能来强制安装附加要求.

Sometimes a project has "recommended" dependencies, that are not required for all uses of the project. For example, a project might offer optional PDF output if ReportLab is installed, and reStructuredText support if docutils is installed. These optional features are called "extras", and setuptools allows you to define their requirements as well. In this way, other projects that require these optional features can force the additional requirements to be installed, by naming the desired extras in their install_requires.

最大的不同是extras_require中的需求只在需要的时候安装:

The biggest difference is that the requirements in extras_require are only installed as needed:

这些要求不会自动安装,除非另一个包依赖于它们(直接或间接),通过在相关项目名称后的方括号中包含所需的额外".(或者,如果 EasyInstall 命令行的需求规范中列出了附加功能.)

These requirements will not be automatically installed unless another package depends on them (directly or indirectly) by including the desired "extras" in square brackets after the associated project name. (Or if the extras were listed in a requirement spec on the EasyInstall command line.)

总结一下:

  • 如果依赖项是必要来运行您的项目,请将其放入install_requires.它们将始终被安装.
  • 如果您的项目具有添加依赖项的可选功能,请将这些依赖项放在 extras_require 中.除非用户或其他软件包需要该功能,否则将不会安装这些依赖项.
  • If the dependency is necessary to run your project, put it in install_requires. They will always be installed.
  • If your project has optional features which add dependencies, put those dependencies in extras_require. Those dependencies will not be installed unless that feature is called for by the user or another package.

这篇关于setup.py 中的 extras_require() 和 install_requires() 的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆