[Python-checkins] cpython: Issue #27853: Add section headers to the importlib example docs
brett.cannon
python-checkins at python.org
Thu Sep 8 13:12:53 EDT 2016
https://hg.python.org/cpython/rev/a547d43c7258
changeset: 103318:a547d43c7258
user: Brett Cannon <brett at python.org>
date: Thu Sep 08 10:12:47 2016 -0700
summary:
Issue #27853: Add section headers to the importlib example docs
files:
Doc/library/importlib.rst | 19 +++++++++++++++++++
1 files changed, 19 insertions(+), 0 deletions(-)
diff --git a/Doc/library/importlib.rst b/Doc/library/importlib.rst
--- a/Doc/library/importlib.rst
+++ b/Doc/library/importlib.rst
@@ -1353,6 +1353,9 @@
Examples
--------
+Importing programmatically
+''''''''''''''''''''''''''
+
To programmatically import a module, use :func:`importlib.import_module`.
::
@@ -1360,6 +1363,10 @@
itertools = importlib.import_module('itertools')
+
+Checking if a module can be imported
+''''''''''''''''''''''''''''''''''''
+
If you need to find out if a module can be imported without actually doing the
import, then you should use :func:`importlib.util.find_spec`.
::
@@ -1380,6 +1387,10 @@
# Adding the module to sys.modules is optional.
sys.modules[name] = module
+
+Importing a source file directly
+''''''''''''''''''''''''''''''''
+
To import a Python source file directly, use the following recipe
(Python 3.4 and newer only)::
@@ -1398,6 +1409,10 @@
# by name later.
sys.modules[module_name] = module
+
+Setting up an importer
+''''''''''''''''''''''
+
For deep customizations of import, you typically want to implement an
:term:`importer`. This means managing both the :term:`finder` and :term:`loader`
side of things. For finders there are two flavours to choose from depending on
@@ -1428,6 +1443,10 @@
# of priority.
sys.path_hooks.append(SpamPathEntryFinder.path_hook(loader_details))
+
+Approximating :func:`importlib.import_module`
+'''''''''''''''''''''''''''''''''''''''''''''
+
Import itself is implemented in Python code, making it possible to
expose most of the import machinery through importlib. The following
helps illustrate the various APIs that importlib exposes by providing an
--
Repository URL: https://hg.python.org/cpython
More information about the Python-checkins
mailing list