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.)
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.
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.
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.
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'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With