Stolen from _parsed_pkg_info
in DistInfoDistribution
:
dist = pkg_resources.get_distribution('some_python_module')
pkginfo = dist.get_metadata(dist.PKG_INFO)
parsed = email.parser.Parser().parsestr(pkginfo)
print(parsed['Name'])
Not sure why this method is not in Distribution
as it appears to work for both DistInfoDistribution
and EggInfoDistribution
-- this is what dist.PKG_INFO
is for by the way, this variable differs between those two ('METADATA'
for .dist-info
and 'PKG-INFO'
for .egg-info
).
Little note: parsestr
returns a FeedParser
object which outputs the parsed input feed when you call print
on it -- do not get confused by this, it's not a str
. Also note that spaces are not allowed in key names by the RFC and lead to silent parser errors!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…