New submission from Terry J. Reedy:

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.)

----------
components: Interpreter Core, Library (Lib)
messages: 270438
nosy: brett.cannon, eric.snow, ncoghlan, terry.reedy
priority: normal
severity: normal
stage: test needed
status: open
title: Dotted name re-import does not rebind after deletion
type: behavior
versions: Python 2.7, Python 3.5, Python 3.6

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue27515>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to