Message270438
https://docs.python.org/3/reference/simple_stmts.html#the-import-statement says that import <module>, where <module> can optionally be a dotted name referring to a module within a package, does two things:
1. Find a module object corresponding to <module>, creating it if necessary.
2. Bind the object to the name in the local namespace. In short, 'import x' is shorthand for "x = __import__('x', ...)".
AFAIK, this works for simple names, including re-imports after name deletion.
>>> import email; email
<module 'email' from 'C:\\Programs\\Python36\\lib\\email\\__init__.py'>
>>> del email
>>> import email; email
<module 'email' from 'C:\\Programs\\Python36\\lib\\email\\__init__.py'>
However, the same is not true for dotted names.
>>> import email.charset; email.charset
<module 'email.charset' from 'C:\\Programs\\Python36\\lib\\email\\charset.py'>
>>> del email.charset
>>> import email.charset; email.charset
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
import email.charset; email.charset
AttributeError: module 'email' has no attribute 'charset'
It appears that for dotted names, when step 1 is cut short by finding the cached module, step 2 is (improperly) omitted. I consider this a bug in the code rather than the doc. I think the name binding should not depend on how the module was found. I don't know whether the bug is somewhere in importlib or in the core machinery that uses it.
This bug, in relation to tkinter package modues, prevented and AFAIK prevents me from fixing the bug of #25507 in 3.x versions prior to 3.6 (Tkinter in 2.x was not a package). (For 3.6, I can and will refactor idlelib to eliminate the need for the deletion by preventing excess imports.) |
|
Date |
User |
Action |
Args |
2016-07-14 20:48:35 | terry.reedy | set | recipients:
+ terry.reedy, brett.cannon, ncoghlan, eric.snow |
2016-07-14 20:48:35 | terry.reedy | set | messageid: <1468529315.51.0.393890498474.issue27515@psf.upfronthosting.co.za> |
2016-07-14 20:48:35 | terry.reedy | link | issue27515 messages |
2016-07-14 20:48:35 | terry.reedy | create | |
|