I'm currently using code similar to this
# Returns PKG-INFO in "email format", meaning it looks a little like this:
# Name: some_python_module
# Version: 1.2.3
dist = pkg_resources.get_distribution('some_python_module')
pkginfo = dist.get_metadata(dist.PKG_INFO)
d = dict(line.split(': ', 1) for line in pkginfo.split('\n') if ': ' in line)
print(d['Name'])
I'm wondering if there is a "clean" way to do this key lookup for a Distribution object?
Note I am using custom properties here so doing dist.project_name or dist.version are not enough in my particular case.
Stolen from
_parsed_pkg_infoinDistInfoDistribution:Not sure why this method is not in
Distributionas it appears to work for bothDistInfoDistributionandEggInfoDistribution-- this is whatdist.PKG_INFOis for by the way, this variable differs between those two ('METADATA'for.dist-infoand'PKG-INFO'for.egg-info).Little note:
parsestrreturns aFeedParserobject which outputs the parsed input feed when you callprinton it -- do not get confused by this, it's not astr. Also note that spaces are not allowed in key names by the RFC and lead to silent parser errors!