classification
Title: Clarify import statement documentation regarding what gets bound
Type: Stage: resolved
Components: Documentation Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: brett.cannon Nosy List: Arfrever, benjamin.peterson, brett.cannon, docs@python, eric.snow, python-dev
Priority: normal Keywords:

Created on 2012-04-20 04:31 by eric.snow, last changed 2012-04-20 19:58 by brett.cannon. This issue is now closed.

Messages (5)
msg158793 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2012-04-20 04:31
(see #14609 and #14582)

http://docs.python.org/dev/reference/simple_stmts.html#the-import-statement

The specification for the import statement says the following:

"The first form of import statement binds the module name in the local namespace to the module object".

This is not the case and has not been the case perhaps ever.  Instead of getting bound to the module (returned by loader.load_module() during import), the name is bound to the value at sys.modules[module_name].

Normally two approaches yield the same result.  However, if during import the sys.modules value for the module is assigned to something else, the behavior deviates from the specfication.  See #14609 for an example.

Considering the backwards-compatibility issues, the language reference should be updated to reflect the actual behavior.
msg158833 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2012-04-20 15:13
So I think the sentence is saying exactly what is going on, but you want clarification that the module object is originating from sys.modules specifically. So you could change the sentence to "The first form of import statement binds the module name in the local namespace to the module object as found in sys.modules."
msg158841 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2012-04-20 15:27
Sounds mostly right.  sys.modules[name] won't necessarily contain a module object, as Benjamin brought up in issue14609.
msg158843 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2012-04-20 15:34
OK, so you say "to the object found in sys.modules."
msg158879 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-04-20 19:58
New changeset 150bd84e737e by Brett Cannon in branch 'default':
Issue #14628: Document the fact that import always returns the module
http://hg.python.org/cpython/rev/150bd84e737e
History
Date User Action Args
2012-04-20 19:58:32brett.cannonsetstatus: open -> closed
assignee: docs@python -> brett.cannon
resolution: fixed
stage: resolved
2012-04-20 19:58:04python-devsetnosy: + python-dev
messages: + msg158879
2012-04-20 15:34:07brett.cannonsetmessages: + msg158843
2012-04-20 15:27:57eric.snowsetmessages: + msg158841
2012-04-20 15:13:51brett.cannonsetmessages: + msg158833
2012-04-20 06:14:51Arfreversetnosy: + Arfrever
2012-04-20 04:31:07eric.snowcreate