Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

importlib.metadata

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:

Python
>>> 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.

files() returns a list of Path objects. These give you a convenient way of looking into the source code of a package, using read_text(). The following example prints out __init__.py from the realpython-reader package:

Python
>>> [p for p in metadata.files("realpython-reader") if p.suffix == ".py"]
[PackagePath('reader/__init__.py'), PackagePath('reader/__main__.py'),
 PackagePath('reader/feed.py'), PackagePath('reader/viewer.py')]

>>> init_path = _[0]  # Underscore access last returned value in the REPL
>>> print(init_path.read_text())
"""Real Python feed reader

Import the `feed` module to work with the Real Python feed:

    >>> from reader import feed
    >>> feed.get_titles()
    ['Logging in Python', 'The Best Python Books', ...]

See https://github.com/realpython/reader/ for more information
"""

# Version of realpython-reader package
__version__ = "1.0.0"

...

You can also access package dependencies:

Python
>>> metadata.requires("realpython-reader")
['feedparser', 'html2text', 'importlib-resources', 'typing']

requires() lists the dependencies of a package. You can see that realpython-reader for instance uses feedparser in the background to read and parse a feed of articles.

See the documentation for more information about importlib.metadata.

00:00 This video covers a new module, importlib.metadata. Python 3.8 adds a new module to the standard library. You can use importlib.metadata to access information about installed packages within your Python installation.

00:16 Together with its companion module importlib.resources it improves on the functionality of the older pkg_resources. Let me have you explore it a bit. Here inside the REPL, go ahead and import, from importlib import metadata.

00:34 And what you’re going to do is get some information about pip, such as the version. And here you can read about the .version() method and how it will grab the version string for the named package.

00:45 So here, it finds that I have pip version '19.3.1' installed. In fact, I’ll create a new object called pip_metadata and then use metadata to pull—well, you can see a variety of methods that are available.

00:58 I want you to use the one called .metadata()—ha, how meta. metadata.metadata(), which you can see here grabs the metadata for the named package of "pip".

01:10 You could use the list() function to print this out, what it created. So, list(pip_metadata), and here you can see a variety of items that came through in that list.

01:19 So one of the things you could look at here is you could find from pip_metadata, what’s the "Home-page" for pip?

01:28 You can see it here. Using that key, you can see here that it’s the web address for it. You could also learn what version of Python is required.

01:42 Here, it says it needs to be greater than 2.7, and not any of these versions. It has to be at least higher than 3.4—so 3.5 or higher. And then you can learn a bit more about the package using len() to see how many files make up your installation of pip. For my installation, it’s 670 files.

02:03 To try out some of these other features, go ahead and exit the REPL. You don’t have to install this if you don’t want to, but I’m going to for this example pip install the realpython-reader.

02:13 And the reason is, it’s a fairly simple installation.

02:20 Okay. Going back into my REPL, metadata.files() can provide a list of all the path objects. I’m going to create [p for p in metadata.files()] for the realpython-reader that I just installed. And in this case, if p.suffix is equal to a Python file, so ".py"ope, I need to re-import metadata. Okay. Now I can try that again.

02:49 And here you can see one, two, three, four files. And what I’m going to use here, there’s a convenient way that you can look into the source code of a package using the function .read_text().

02:59 So I’m going to use a combination of a couple things. Here, I’m going to create something called the init_path. It’s going to read from the last thing that I brought in, using underscore (_), so it’s going to access the last returned value in the REPL.

03:11 I’m going to grab the first item from that. So again…

03:21 And from init_path, I’ll have it read the text. So here, you can see it reading the text out from the __init__.py file.

03:35 One other feature that you can use, metadata.requires() will list out the dependencies of a package. So in the case of the realpython-reader, what are its dependencies?

03:46 And here you can see the four that it requires. Pretty cool. In the next video, I’ll have you dive into new and improved math and statistics functions.

Become a Member to join the conversation.