This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: __import__ documentation obsolete
Type: Stage:
Components: Documentation Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: brett.cannon, georg.brandl, mrts, steven.daprano
Priority: normal Keywords: patch

Created on 2008-11-29 04:40 by steven.daprano, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
import-docs.diff georg.brandl, 2008-12-05 18:22
Messages (6)
msg76582 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2008-11-29 04:40
The documentation for __import__ says that it primarily exists "so that
you can replace it with another function that has a compatible
interface, in order to change the semantics of the import statement".
http://docs.python.org/library/functions.html

That is no longer the case. The recommended way to do that is with the
new import hooks, and the docs should reflect that.

A common use for __import__ is for when you don't know the module name
until runtime. That too should be mentioned.
msg76637 - (view) Author: Mart Sõmermaa (mrts) Date: 2008-11-30 12:38
Also, the examples that clarify __import__ behaviour by Nick Coghlan
should be added:

http://mail.python.org/pipermail/python-dev/2008-November/083735.html

---

"from foo.bar import baz" ---->

<stack top> = __import__('foo.bar', globals(), locals(), ['baz'], -1)
baz = <stack top>.baz

When there are multiple names being imported or an 'as' clause is
involved, I hope the reasons for doing it this way become more obvious:

"from foo.bar import baz, bob" ---->

<stack top> = __import__('foo.bar', globals(), locals(), ['baz', 'bob'], -1)
baz = <stack top>.baz
bob = <stack top>.bob

"from foo.bar import baz as bob" ---->

<stack top> = __import__('foo.bar', globals(), locals(), ['baz', 'bob'], -1)
bob = <stack top>.baz

---

And the "winning idiom" by Hrvoje Niksic for accessing module 'z', given
name hierarchy 'x.y.z' should be documented as well:

>>> import sys
>>> __import__('x.y.z')
>>> mod = sys.modules['x.y.z']
msg77051 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-12-05 18:22
Attached is a proposed rewrite of the __import__ documentation.
msg77052 - (view) Author: Mart Sõmermaa (mrts) Date: 2008-12-05 18:25
Brett, don't you think the 

>>> import sys
>>> __import__('x.y.z')
>>> mod = sys.modules['x.y.z']

idiom should be recommended instead of/additionally to the lengthy
getattr() one?
msg77056 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2008-12-05 19:19
Yes, the simple __import__/sys.modules idiom should be the suggested
idiom until a proper function is provided in the standard library.
msg77272 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-12-07 22:42
Changed patch to document sys.modules trick and applied in r67654.
History
Date User Action Args
2022-04-11 14:56:41adminsetgithub: 48707
2008-12-07 22:42:20georg.brandlsetstatus: open -> closed
resolution: fixed
messages: + msg77272
2008-12-05 19:19:56brett.cannonsetnosy: + brett.cannon
messages: + msg77056
2008-12-05 18:25:23mrtssetmessages: + msg77052
2008-12-05 18:22:26georg.brandlsetfiles: + import-docs.diff
keywords: + patch
messages: + msg77051
2008-11-30 12:38:21mrtssetnosy: + mrts
messages: + msg76637
2008-11-29 04:40:53steven.dapranocreate