Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 couldn't get it. Both are used for installing Python dependencies, but what's the difference between them?

like image 953
Harish R Avatar asked Dec 21 '16 17:12

Harish R


People also ask

What is Install_requires in setup py?

install_requires is a section within the setup.py file in which you need to input a list of the minimum dependencies needed for a project to run correctly on the target operating system (such as ubuntu). When pip runs setup.py, it will install all of the dependencies listed in install_requires.

What is Package_dir in setup py?

package_dir = {'': 'lib'} in your setup script. The keys to this dictionary are package names, and an empty package name stands for the root package. The values are directory names relative to your distribution root.

How do I set python version in setup py?

As the setup.py file is installed via pip (and pip itself is run by the python interpreter) it is not possible to specify which Python version to use in the setup.py file.

How install pip using setup py?

Installing Python Packages with Setup.py To install a package that includes a setup.py file, open a command or terminal window and: cd into the root directory where setup.py is located. Enter: python setup.py install.


2 Answers

According to the setuptools documentation,

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.

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:

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.

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

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.)

So to summarize:

  • 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.
like image 178
user812786 Avatar answered Oct 21 '22 13:10

user812786


I'm not sure of the official usage, but I use extras_require() to specify conditional dependencies.

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.

This article explains it nicely: Conditional Python Dependencies

like image 27
Jay Avatar answered Oct 21 '22 13:10

Jay