Is this "convenience wrapper": https://docs.python.org/2/library/importlib.html just providing another way of writing:
import path.to.my.module
Is there something is does that you can't do with a normal import statement?
Is this "convenience wrapper": https://docs.python.org/2/library/importlib.html just providing another way of writing:
import path.to.my.module
Is there something is does that you can't do with a normal import statement?
It allows you to import modules which you do not know the name at coding time.
For instance when my application starts, I walk through a directory structure and load the modules as I discover them.
In Python 2.7,
importlib
isn't super useful. In fact, its only feature is theimport_module
function, which enables you to import a module from a string name:Note that you could do the same with the
__import__
built-in, but usingimport_module
is generally preferred.In Python versions 3.1 and greater however, the purpose of
importlib
has been expanded. According to the documentation:Summarized,
importlib
now allows you to access the internals of Python's import-statement, build custom finders, loaders, and importers, setup import hooks, and much more.In fact, as of version 3.3,
importlib
holds the implementation of the import-statement itself. You can read about this on the What's New in Python 3.3 page under Usingimportlib
as the Implementation of Import.Also,
importlib
will be replacing older modules related to importing in future versions of Python. For example, the oldimp
module was deprecated in version 3.4 in favor ofimportlib
.With all of this in mind, I guess it's safe to say that
importlib
is pretty important in modern Python. ;)