|
| 1 | +:mod:`importlib` -- An implementation of :keyword:`import` |
| 2 | +========================================================== |
| 3 | + |
| 4 | +.. module:: importlib |
| 5 | + :synopsis: An implementation of the import machinery. |
| 6 | + |
| 7 | +.. moduleauthor:: Brett Cannon <brett@python.org> |
| 8 | +.. sectionauthor:: Brett Cannon <brett@python.org> |
| 9 | + |
| 10 | +.. versionadded:: 3.1 |
| 11 | + |
| 12 | + |
| 13 | +Introduction |
| 14 | +------------ |
| 15 | + |
| 16 | +The purpose of the :mod:`importlib` package is two-fold. One is to provide an |
| 17 | +implementation of the :keyword:`import` statement (and thus, by extension, the |
| 18 | +:func:`__import__` function) in Python source code. This provides an |
| 19 | +implementaiton of :keyword:`import` which is portable to any Python |
| 20 | +interpreter. This also provides a reference implementation which is easier to |
| 21 | +read than one in a programming language other than Python. |
| 22 | + |
| 23 | +Two, the components to implement :keyword:`import` can be exposed in this |
| 24 | +package, making it easier for users to create their own custom objects (known |
| 25 | +generically as importers) to participate in the import process. Details on |
| 26 | +providing custom importers can be found in :pep:`302`. |
| 27 | + |
| 28 | +.. seealso:: |
| 29 | + |
| 30 | + :ref:`import` |
| 31 | + The language reference for the :keyword:`import` statement. |
| 32 | + |
| 33 | + `Packages specification <http://www.python.org/doc/essays/packages.html>`__ |
| 34 | + Original specification of packages. Some semantics have changed since |
| 35 | + the writing of this document (e.g. redirecting based on :keyword:`None` |
| 36 | + in :data:`sys.modules`). |
| 37 | + |
| 38 | + The :func:`.__import__` function |
| 39 | + The built-in function for which the :keyword:`import` statement is |
| 40 | + syntactic sugar for. |
| 41 | + |
| 42 | + :pep:`235` |
| 43 | + Import on Case-Insensitive Platforms |
| 44 | + |
| 45 | + :pep:`263` |
| 46 | + Defining Python Source Code Encodings |
| 47 | + |
| 48 | + :pep:`302` |
| 49 | + New Import Hooks. |
| 50 | + |
| 51 | + :pep:`328` |
| 52 | + Imports: Multi-Line and Absolute/Relative |
| 53 | + |
| 54 | + :pep:`366` |
| 55 | + Main module explicit relative imports |
| 56 | + |
| 57 | + :pep:`3128` |
| 58 | + Using UTF-8 as the Default Source Encoding |
| 59 | + |
| 60 | + |
| 61 | +Functions |
| 62 | +--------- |
| 63 | + |
| 64 | +.. function:: __import__(name, globals={}, locals={}, fromlist=\[\], level=0) |
| 65 | + |
| 66 | + An implementation of the built-in :func:`__import__` function. See the |
| 67 | + built-in function's documentation for usage instructions. |
| 68 | + |
| 69 | +.. function:: import_module(name, package=None) |
| 70 | + |
| 71 | + Import a module. The ``name`` argument specifies what module to |
| 72 | + import in absolute or relative terms |
| 73 | + (e.g. either ``pkg.mod`` or ``..mod``). If the name is |
| 74 | + specified in relative terms, then the ``package`` argument must be |
| 75 | + specified to the package which is to act as the anchor for resolving the |
| 76 | + package name (e.g. ``import_module('..mod', 'pkg.subpkg')`` will import |
| 77 | + ``pkg.mod``). The specified module will be inserted into |
| 78 | + :data:`sys.modules` and returned. |
0 commit comments