Difference between extras_require () and install_requires () in setup.py?

I am trying to understand the difference between extras_require() and install_requires() in the setup.py file, but could not get it. Both are used to install Python dependencies, but what's the difference between the two?

+6
source share
2 answers

According to setuptools documentation ,

extras_require
A dictionary that matches the names "extras" (optional features of your project) with strings or lists of strings indicating which other distributions should be installed to support these features.

and

install_requires
A string or list of strings that determine which other distributions should be installed, when available.

The Advanced section (additional functions with its dependencies) declares in detail the following:

Sometimes a project has “recommended” dependencies that are not required for all uses of the project. For example, a project may offer additional PDF output if ReportLab is installed, and reStructuredText support if docutils is installed. These advanced features are called “advanced features,” and setuptools also lets you define their requirements. Thus, other projects that require these additional functions can force additional requirements by naming the desired additional functions in their install_requires .

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

These requirements will not be automatically set if another package does not depend on them (directly or indirectly), including the required "additional" in square brackets after the corresponding project name. (Or if additional parameters were specified in the requirements specification on the EasyInstall command line.)

So, we summarize:

  • If <project> is required , enter install_requires . They will always be installed.
  • If your project has additional functions that add dependencies, put these dependencies in extras_require . These dependencies will not be installed unless this function is called by the user or another package.
+4
source

I am not sure about official use, but I use extras_require() to indicate conditional dependencies.

In my case -

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

Theoretically, this should be accessible through install_requires() , but it only works the way it should start with the X.XX version (a few statements about which version starts to understand it correctly) setuptools .

This article explains this beautifully: Conditional Python dependencies

+2
source

Source: https://habr.com/ru/post/1013399/


All Articles