Skip to content

Commit afccd63

Browse files
committed
Document the (very small) public API for importlib. As time goes on and some
key refactorings occur more of the API will be exposed and documented.
1 parent df50106 commit afccd63

5 files changed

Lines changed: 111 additions & 30 deletions

File tree

Doc/library/importlib.rst

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.

Doc/library/modules.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ The full list of modules described in this chapter is:
1818
pkgutil.rst
1919
modulefinder.rst
2020
runpy.rst
21+
importlib.rst

Lib/importlib/NOTES

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
to do
22
/////
33

4-
* Write importlib.__import__
4+
* Expose resolve_name().
55

6-
* Document
7-
+ Package.
6+
* Backport to Python 2.7.
87
+ import_module
9-
+ __import__
8+
+ resolve_name
109

1110
* Create reasonable base tests that all finders and loaders must pass so
1211
that various implementations can just subclass as needed.
@@ -42,7 +41,7 @@ to do
4241
- Absolute name from sys.path.
4342
- Relative name from sys.path.
4443

45-
* Public API (w/ docs!)
44+
* Public API to expose (w/ docs!)
4645
+ abc
4746
- Finder
4847
* find_module
@@ -72,9 +71,5 @@ to do
7271
- Source/bytecode importers
7372
* SourceFinder
7473
* (?) Loader
75-
+ __init__
76-
- __import__
77-
- import_module (backport to 2.7)
78-
- resolve_name (backport to 2.7)
7974

8075
* Bootstrap importlib as implementation of builtins.__import__

Lib/importlib/__init__.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,6 @@ def _r_long(int_bytes):
7979
return x
8080

8181

82-
def import_module(name, package=None):
83-
"""Import a module.
84-
85-
The 'package' argument is used to resolve relative import names. Typically
86-
this is the __package__ attribute of the module making the function call.
87-
88-
"""
89-
if name.startswith('.'):
90-
if not package:
91-
raise TypeError("relative imports require the 'package' argument")
92-
level = 0
93-
for character in name:
94-
if character != '.':
95-
break
96-
level += 1
97-
name = Import._resolve_name(name[level:], package, level)
98-
__import__(name)
99-
return sys.modules[name]
100-
101-
102-
10382
# Required built-in modules.
10483
try:
10584
import posix as _os
@@ -130,4 +109,30 @@ def import_module(name, package=None):
130109
marshal._w_long = _w_long
131110
marshal._r_long = _r_long
132111

112+
113+
__import__ = _bootstrap.Import().__call__
114+
115+
116+
def import_module(name, package=None):
117+
"""Import a module.
118+
119+
The 'package' argument is required when performing a relative import. It
120+
specifies the package to use as the anchor point from which to resolve the
121+
relative import to an absolute import.
122+
123+
"""
124+
if name.startswith('.'):
125+
if not package:
126+
raise TypeError("relative imports require the 'package' argument")
127+
level = 0
128+
for character in name:
129+
if character != '.':
130+
break
131+
level += 1
132+
name = Import._resolve_name(name[level:], package, level)
133+
__import__(name)
134+
return sys.modules[name]
135+
136+
137+
133138
from ._bootstrap import *

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ Core and Builtins
134134
Library
135135
-------
136136

137+
- Add the importlib package.
138+
137139
- Issue #4301: Patch the logging module to add processName support, remove
138140
_check_logger_class from multiprocessing.
139141

0 commit comments

Comments
 (0)