简体   繁体   English

setup.py 中的 extras_require() 和 install_requires() 有什么区别?

[英]What is the difference between `extras_require()` and `install_requires()` in setup.py?

I am trying to understand the difference between extras_require() and install_requires() in setup.py but haven't been able to get it.我试图了解 setup.py 中extras_require()install_requires()之间的区别,但一直无法理解。 Both are used for installing Python dependencies, but what's the difference between them?都是用来安装Python依赖的,它们有什么区别呢?

According to the setuptools documentation ,根据setuptools 文档

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.一个字典将“extras”(项目的可选功能)的名称映射到字符串或字符串列表,指定必须安装哪些其他发行版才能支持这些功能。

and

install_requires
A string or list of strings specifying what other distributions need to be installed when this one is.一个字符串或字符串列表,指定在安装此发行版时需要安装哪些其他发行版。

The section on Declaring “Extras” (optional features with their own dependencies) elaborates on this: 声明“Extras”(具有自己的依赖项的可选功能)部分详细说明了这一点:

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.例如,如果安装了 ReportLab,项目可能会提供可选的 PDF 输出,如果安装了 docutils,则项目可能会提供 reStructuredText 支持。 These optional features are called “extras”, and setuptools allows you to define their requirements as well.这些可选功能称为“附加功能”,setuptools 也允许您定义它们的要求。 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 .通过这种方式,需要这些可选功能的其他项目可以通过在其install_requires命名所需的附加功能来强制安装附加要求。

The biggest difference is that the requirements in extras_require are only installed as needed:最大的区别是extras_require中的需求只在需要的时候安装:

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.) (或者,如果 EasyInstall 命令行的需求规范中列出了附加功能。)

So to summarize:所以总结一下:

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

I'm not sure of the official usage, but I use extras_require() to specify conditional dependencies.我不确定官方的用法,但我使用extras_require()来指定条件依赖项。

In my case -就我而言 -

extras_require={":python_version<'3.5'": ["scandir"]}

Theoretically, this should be available via install_requires() itself, but it only works as it should starting version X.XX (several claims as to which version starts getting it right) of setuptools .从理论上讲,这应该可以通过install_requires()本身获得,但它只能在启动setuptools X.XX 版本(关于哪个版本开始正确) setuptools

This article explains it nicely: Conditional Python Dependencies这篇文章很好地解释了它: Conditional Python Dependencies

install_requires are the dependency packages which are installed no matter what. install_requires是无论如何安装的依赖包。

pip install mypackage

extras_require are optional, and need to be specified at install time. extras_require是可选的,需要在安装时指定。 This is a feature of pip .这是pip一个特性。

pip install mypackage[extra]
pip install mypackage[develop]
pip install mypackage[extra,develop]

A good example is https://pypi.org/project/imgui/ which allows you to choose a specific graphics backend.一个很好的例子是https://pypi.org/project/imgui/ ,它允许您选择特定的图形后端。

Refer to PEP 508 to see all possible selectors you can use;请参阅PEP 508以查看您可以使用的所有可能的选择器; eg ; python_version < "2.7"例如; python_version < "2.7" ; python_version < "2.7" . ; python_version < "2.7"

This is a very good question.这个问题问得好。 I was looking for an answer myself, but couldn't find one that satisfied me.我自己也在寻找答案,但找不到让我满意的答案。 So after gaining some experience, here are some examples that can help better understand:所以在积累了一些经验之后,这里有一些例子可以帮助更好地理解:

Suppose our package is foo and it integrates with a users package bar , extending it's functionality.假设我们的包是foo并且它与用户包bar集成,扩展了它的功能。 Our package foo cannot work without bar so it seems like it should be in install_requires , but there is a problem with that.我们的包foo不能在没有bar情况下工作,所以看起来它应该在install_requires ,但是有一个问题。 If, for example, user had version 1.1 of bar installed, then installed our package foo - our package may install version 1.2 of bar which will override users version.例如,如果用户安装了 1.1 版的bar ,则安装了我们的包foo - 我们的包可能安装了bar 1.2 版,这将覆盖用户的版本。 Instead, we put bar in bar section in extras_require .相反,我们将bar放在extras_require bar部分。 In this case user can safely install foo , knowing that it will integrate with his existing version of bar.在这种情况下,用户可以安全地安装foo ,知道它将与他现有的 bar 版本集成。 But what if the user doesn't have bar installed?但是如果用户没有安装bar呢? In this case the user will run pip install foo[bar] .在这种情况下,用户将运行pip install foo[bar]

Another good example is tests.另一个很好的例子是测试。 Very often the tests of your packge use packages like mock or spceific data types (like DataFrame ) which are not mandatory for the use of the package itself.你的包的测试经常使用类似mock或特殊数据类型(如DataFrame )的包,这些包对于包本身的使用不是强制性的。 In this case, you can put all packages required for tests in test section in extras_require .在这种情况下,您可以将测试所需的所有包放在extras_require中的 test 部分。 When you want to run tests in a virtual environment (tox), you can simply write deps=my_package[tests] in the tox.ini file.当您想在虚拟环境 (tox) 中运行测试时,您只需在 tox.ini 文件中写入deps=my_package[tests]即可。

I hope this answer helps.我希望这个答案有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM