In this lesson, you’ll learn about a new module in the standard library in Python 3.8, importlib.metadata
.
Through this module, you can access information about installed packages in your Python installation. Together with its companion module, importlib.resources
, importlib.metadata
improves on the functionality of the older pkg_resources
.
As an example, you can get some information about pip
:
>>> from importlib import metadata
>>> metadata.version("pip")
'19.3.1'
>>> pip_metadata = metadata.metadata("pip")
>>> list(pip_metadata)
['Metadata-Version', 'Name', 'Version', 'Summary', 'Home-page', 'Author',
'Author-email', 'License', 'Keywords', 'Platform', 'Classifier',
'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier',
'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier',
'Classifier', 'Classifier', 'Requires-Python']
>>> pip_metadata["Home-page"]
'https://pip.pypa.io/'
>>> pip_metadata["Requires-Python"]
'>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*'
>>> len(metadata.files("pip"))
670
The currently installed version of pip
is 19.2.3. metadata()
gives access to most of the information that you can see on PyPI. You can for instance see that this version of pip
requires either Python 2.7, or Python 3.5 or higher. With files()
, you get a listing of all files that make up the pip
package. In this case, there are almost 700 files.