@@ -660,41 +660,86 @@ The :keyword:`import` statement
660660 relative_module: "."* `module ` | "."+
661661 name: `identifier `
662662
663- Import statements are executed in two steps: (1) find a module, loading and
664- initializing it if necessary; (2) define a name or names in the local
665- namespace (of the scope where the :keyword: `import ` statement occurs).
666- Step (1) may be performed recursively if the named module is a submodule or
667- subpackage of a parent package.
663+ The basic import statement (no :keyword: `from ` clause) is executed in two
664+ steps:
665+
666+ #. find a module, loading and initializing it if necessary
667+ #. define a name or names in the local namespace for the scope where
668+ the :keyword: `import ` statement occurs.
669+
670+ When the statement contains multiple clauses (separated by
671+ commas) the two steps are carried out separately for each clause, just
672+ as though the clauses had been separated out into individiual import
673+ statements.
674+
675+ The details of the first step, finding and loading modules is described in
676+ greater detail in the section on the :ref: `import system <importsystem >`,
677+ which also describes the various types of packages and modules that can
678+ be imported, as well as all the hooks that can be used to customize
679+ the import system. Note that failures in this step may indicate either
680+ that the module could not be located, *or * that an error occurred while
681+ initializing the module, which includes execution of the module's code.
682+
683+ If the requested module is retrieved successfully, it will be made
684+ available in the local namespace in one of three ways:
685+
686+ * If the module name is followed by :keyword: `as `, then the name
687+ following :keyword: `as ` is bound directly to the imported module.
688+ * If no other name is specified, and the module being imported is a top
689+ level module, the module's name is bound in the local namespace as a
690+ reference to the imported module
691+ * If the module being imported is *not * a top level module, then the name
692+ of the top level package that contains the module is bound in the local
693+ namespace as a reference to the top level package. The imported module
694+ must be accessed using its full qualified name rather than directly
668695
669- The :keyword: `import ` statement comes in two forms differing on whether it
670- uses the :keyword: `from ` keyword. The first form (without :keyword: `from `)
671- repeats these steps for each identifier in the list. The form with
672- :keyword: `from ` performs step (1), and then performs step (2) repeatedly.
673-
674- The details of step (1), finding and loading modules is described in greater
675- detail in the section on the :ref: `import system <importsystem >`, which
676- also describes the various types of packages and modules that can be imported,
677- as well as all the hooks that can be used to customize Python's import.
678-
679- When step (1) finishes without raising an exception, step (2) can begin.
680-
681- The first form of :keyword: `import ` statement binds the module name in the
682- local namespace to the module object, and then goes on to import the next
683- identifier, if any. If the module name is followed by :keyword: `as `, the name
684- following :keyword: `as ` is used as the local name for the module.
685696
686697.. index ::
687698 pair: name; binding
699+ keyword: from
688700 exception: ImportError
689701
690- The :keyword: `from ` form does not bind the module name: it goes through the
691- list of identifiers, looks each one of them up in the module found in step
692- (1), and binds the name in the local namespace to the object thus found. As
693- with the first form of :keyword: `import `, an alternate local name can be
694- supplied by specifying ":keyword: `as ` localname". If a name is not found,
695- :exc: `ImportError ` is raised. If the list of identifiers is replaced by a
696- star (``'*' ``), all public names defined in the module are bound in the local
697- namespace of the :keyword: `import ` statement.
702+ The :keyword: `from ` form uses a slightly more complex process:
703+
704+ #. find the module specified in the :keyword: `from ` clause loading and
705+ initializing it if necessary;
706+ #. for each of the identifiers specified in the :keyword: `import ` clauses:
707+
708+ #. check if the imported module has an attribute by that name
709+ #. if not, attempt to import a submodule with that name and then
710+ check the imported module again for that attribute
711+ #. if the attribute is not found, :exc: `ImportError ` is raised.
712+ #. otherwise, a reference to that value is bound in the local namespace,
713+ using the name in the :keyword: `as ` clause if it is present,
714+ otherwise using the attribute name
715+
716+ Examples::
717+
718+ import foo # foo imported and bound locally
719+ import foo.bar.baz # foo.bar.baz imported, foo bound locally
720+ import foo.bar.baz as fbb # foo.bar.baz imported and bound as fbb
721+ from foo.bar import baz # foo.bar.baz imported and bound as baz
722+ from foo import attr # foo imported and foo.attr bound as attr
723+
724+ If the list of identifiers is replaced by a star (``'*' ``), all public
725+ names defined in the module are bound in the local namespace for the scope
726+ where the :keyword: `import ` statement occurs.
727+
728+ .. index :: single: __all__ (optional module attribute)
729+
730+ The *public names * defined by a module are determined by checking the module's
731+ namespace for a variable named ``__all__ ``; if defined, it must be a sequence
732+ of strings which are names defined or imported by that module. The names
733+ given in ``__all__ `` are all considered public and are required to exist. If
734+ ``__all__ `` is not defined, the set of public names includes all names found
735+ in the module's namespace which do not begin with an underscore character
736+ (``'_' ``). ``__all__ `` should contain the entire public API. It is intended
737+ to avoid accidentally exporting items that are not part of the API (such as
738+ library modules which were imported and used within the module).
739+
740+ The :keyword: `from ` form with ``* `` may only occur in a module scope.
741+ Attempting to use it in class or function definitions will raise a
742+ :exc: `SyntaxError `.
698743
699744.. index :: single: __all__ (optional module attribute)
700745
0 commit comments