Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I list the extra features of a Python package

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.

like image 990
Fabich Avatar asked Aug 14 '20 10:08

Fabich


People also ask

What are python package extras?

Python extras are a way for Python projects to declare that extra dependencies are required for additional functionality. For example, requests has several standard dependencies (e.g. urllib3 ). But it also declares an extra named requests[security] , which lists additional dependencies (e.g. cryptography ).

Which command is used to list all the packages installed in your system in Python?

Pipenv. This command will list all packages installed, including any dependencies that are found in a Pipfile.


Video Answer


1 Answers

There are two open feature requests in pip about this:

  • #3797 - pip show doesn't handle extras_requires
  • #4824 - Add support for outputting a list of extras and their requirements.

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"')>
like image 133
M57 Avatar answered Oct 01 '22 22:10

M57