Search code examples
pythonsetuptoolsdistribute

How can I get the author name, project description etc from a Distribution object in pkg_resources?


pkg_resources api let's you get Distribution objects that represent egg distributions inside a directory. I can trivially get the project name and version of the distribution with dist.project_name and dist.version, however I'm lost on how I can get the other metadata that's usually specified in the setup script (like author, description, etc.)


Solution

  • I was looking how to do the same thing. This is what I came up with. It is probably not the best solution, but seems to work.

    # get the raw PKG-INFO data
    from pkg_resources import get_distribution
    pkgInfo = get_distribution('myapp').get_metadata('PKG-INFO')
    
    # parse it using email.Parser
    from email import message_from_string
    msg = message_from_string(pkgInfo)
    print(msg.items())
    
    # optional: convert it to a MultiDict
    from webob.multidict import MultiDict
    metadata = MultiDict(msg)
    print(metadata.get('Author'))
    print(metadata.getall('Classifier'))