importlib Package

Let's learn about Python's importlib package and how it can be used.

We'll cover the following

Overview of importlib

Python provides the importlib package as part of its standard library of modules. Its purpose is to provide the implementation of Python’s import statement (and the _import_() function). In addition, importlib gives the programmer the ability to create their own custom objects (AKA an importer) that can be used in the import process.

What about imp?

There is another module called imp that provides an interface to the mechanisms behind Python’s import statement. This module was deprecated in Python 3.4. It is intended that importlib should be used in its place.

This module is pretty complicated, so we’ll be limiting the scope of this chapter to the following topics:

  • Dynamic imports
  • Checking if a module can be imported
  • Importing from the source file itself
  • A clever 3rd party module called import_from_github_com

Get hands-on with 1200+ tech skills courses.