Python 3.1 includes the
importlib
package, a re-implementation of the logic underlying Python’s import
statement. importlib
is useful for implementors of Python interpreters and to users who wish to write new importers that can participate in the import process. Python 2.7 doesn’t contain the completeimportlib
package, but instead has a tiny subset that contains a single function, import_module()
.import_module(name, package=None)
imports a module. name is a string containing the module or package’s name. It’s possible to do relative imports by providing a string that begins with a .
character, such as ..utils.errors
. For relative imports, the package argument must be provided and is the name of the package that will be used as the anchor for the relative import. import_module()
both inserts the imported module into sys.modules
and returns the module object.
Here are some examples:
importlib
was implemented by Brett Cannon and introduced in Python 3.1
No comments:
Post a Comment