mirror of
https://github.com/python/cpython.git
synced 2025-11-03 11:23:31 +00:00
Issue #27853: Add section headers to the importlib example docs
This commit is contained in:
parent
7322225a57
commit
2376316857
1 changed files with 19 additions and 0 deletions
|
|
@ -1353,6 +1353,9 @@ an :term:`importer`.
|
||||||
Examples
|
Examples
|
||||||
--------
|
--------
|
||||||
|
|
||||||
|
Importing programmatically
|
||||||
|
''''''''''''''''''''''''''
|
||||||
|
|
||||||
To programmatically import a module, use :func:`importlib.import_module`.
|
To programmatically import a module, use :func:`importlib.import_module`.
|
||||||
::
|
::
|
||||||
|
|
||||||
|
|
@ -1360,6 +1363,10 @@ To programmatically import a module, use :func:`importlib.import_module`.
|
||||||
|
|
||||||
itertools = importlib.import_module('itertools')
|
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
|
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`.
|
import, then you should use :func:`importlib.util.find_spec`.
|
||||||
::
|
::
|
||||||
|
|
@ -1380,6 +1387,10 @@ import, then you should use :func:`importlib.util.find_spec`.
|
||||||
# Adding the module to sys.modules is optional.
|
# Adding the module to sys.modules is optional.
|
||||||
sys.modules[name] = module
|
sys.modules[name] = module
|
||||||
|
|
||||||
|
|
||||||
|
Importing a source file directly
|
||||||
|
''''''''''''''''''''''''''''''''
|
||||||
|
|
||||||
To import a Python source file directly, use the following recipe
|
To import a Python source file directly, use the following recipe
|
||||||
(Python 3.4 and newer only)::
|
(Python 3.4 and newer only)::
|
||||||
|
|
||||||
|
|
@ -1398,6 +1409,10 @@ To import a Python source file directly, use the following recipe
|
||||||
# by name later.
|
# by name later.
|
||||||
sys.modules[module_name] = module
|
sys.modules[module_name] = module
|
||||||
|
|
||||||
|
|
||||||
|
Setting up an importer
|
||||||
|
''''''''''''''''''''''
|
||||||
|
|
||||||
For deep customizations of import, you typically want to implement an
|
For deep customizations of import, you typically want to implement an
|
||||||
:term:`importer`. This means managing both the :term:`finder` and :term:`loader`
|
: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
|
side of things. For finders there are two flavours to choose from depending on
|
||||||
|
|
@ -1428,6 +1443,10 @@ classes defined within this package)::
|
||||||
# of priority.
|
# of priority.
|
||||||
sys.path_hooks.append(SpamPathEntryFinder.path_hook(loader_details))
|
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
|
Import itself is implemented in Python code, making it possible to
|
||||||
expose most of the import machinery through importlib. The following
|
expose most of the import machinery through importlib. The following
|
||||||
helps illustrate the various APIs that importlib exposes by providing an
|
helps illustrate the various APIs that importlib exposes by providing an
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue