Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 239
Te-jé Rodgers Avatar asked May 12 '12 20:05

Te-jé Rodgers


People also ask

What is pkg_ resources in Python?

pkg_resources is a module used to find and manage Python package/version dependencies and access bundled files and resources, including those inside of zipped . egg files.

What is pkg resources?

The pkg_resources module distributed with setuptools provides an API for Python libraries to access their resource files, and for extensible applications and frameworks to automatically discover plugins.

How do you access data packages in Python?

The easiest way is to use the package_data in the setup. To see how you may visit https://docs.python.org/3/distutils/setupscript.html. This will simply allow you to include this data in your library when you package it, for example, to upload it to pypi. Note that the key must be named the same way as the package.


1 Answers

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'))
like image 127
david Avatar answered Oct 18 '22 02:10

david