How to access python package metadata from within the python console?

With python3.8 being released, you might want to use the new importlib.metadata[1] module to parse any installed package's metadata.

Getting the author information would look like this:

>>> from importlib import metadata
>>> metadata.metadata('foo')['Author']  # let's say you called your package 'foo'
'Arne'

And getting the version of your install:

>>> from importlib import metadata
>>> metadata.version('foo')
'0.1.0'

Which is a lot more straight forward than what you had to do before.


[1] Also available as backport for Python2.7 and 3.5+ as importlib-metadata, thanks to @ChrisHunt for pointing that out.


One way to access the metadata is to use pip:

import pip

package = [pckg for pckg in pip.get_installed_distributions() 
            if pckg.project_name == 'package_name'][0]
#  package var will contain some metadata: version, project_name and others.

or pkg_resources

from pkg_resources import get_distribution

pkg = get_distribution('package_name')  # also contains a metadata