How can I list the extra features of a Python package

Viewed 992

Some Python packages have extra features that can be installed by putting them into brackets such as the security extra for the requests package:

pip install requests[security]

Is there a way to list all the extras of a given package ?

I cannot find anything like that in the pip documentation.

1 Answers

There are two open feature requests in pip about this:

In the meantime, a workaround using importlib_metadata's API and working for already installed packages has been provided by jaraco.

Copy-pasting it below:

An even better alternative would be to use importlib_metadata, which has an API.

>>> import importlib_metadata
>>> importlib_metadata.metadata('xonsh').get_all('Provides-Extra')
['linux', 'mac', 'proctitle', 'ptk', 'pygments', 'win']
>>> importlib_metadata.metadata('xonsh').get_all('Requires-Dist')
["distro; extra == 'linux'", "gnureadline; extra == 'mac'", "setproctitle; extra == 'proctitle'", "prompt-toolkit; extra == 'ptk'", "pygments (>=2.2); extra == 'pygments'", "win-unicode-console; extra == 'win'"]

And use packaging to parse them:

>>> req = next(map(packaging.requirements.Requirement, importlib_metadata('xonsh').get_all('Requires-Dist')))
>>> req.name
'distro'
>>> req.specifier
<SpecifierSet('')>
>>> req.extras
set()
>>> req.marker
<Marker('extra == "linux"')>
Related