Search code examples
pythonpython-importlib

How to get all the metadata that have the same name from a python package?


I want to read metadata from a python package to feed a table withsome informations. The package is build using setuptools so all urls are saved under the [project.urls] field in the pyproject.toml file.

My problem is that when using importlib from a python script I only get the first one:

from importlib import metadata
metdata.metdata("pandas")["Project-URL"] # using pandas as an example
>>> 'homepage, https://pandas.pydata.org'

When in fact there are 3 of them:

[project.urls]
homepage = 'https://pandas.pydata.org'
documentation = 'https://pandas.pydata.org/docs/'
repository = 'https://github.com/pandas-dev/pandas'

Is there a way to get all these metadata as a dict or at least a list that I can parse ?


Solution

  • the result of the method is a Message object.This object have a get_all method dedicated to this use case:

    from importlib import metadata
    
    metadata.metadata("pandas").get_all("Project-URL")
    
    >>> ['homepage, https://pandas.pydata.org',
         'documentation, https://pandas.pydata.org/docs/',
         'repository, https://github.com/pandas-dev/pandas']