Issue 24081: Obsolete caveat in reload() docs (original) (raw)

imp.reload() and importlib.reload() docs state::

If a module is syntactically correct but its initialization fails, the first
:keyword:`import` statement for it does not bind its name locally, but does
store a (partially initialized) module object in ``sys.modules``.  To reload
the module you must first :keyword:`import` it again (this will bind the name
to the partially initialized module object) before you can :func:`reload` it.

If I reading that correctly, "initialization" refers to executing the module, so for module containing just::

uninitialized_variable

the following::

>>> import sys
>>> import x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/x.py", line 1, in <module>
    uninitialized_variable
NameError: name 'uninitialized_variable' is not defined

should leave me with a initialized module in sys.modules['x']. However, this is not what happens, in either Python 3.4 or 2.7::

>>> sys.modules['x']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'x'

Here's a patch to remove the caveat in Python 3 docs. If I missed something, and "initialization" refers to something else, it should be clarified.

OK, I can't reproduce it either, neither in python3 nor python2. Brett, is this left over from a long time ago?

Heh. I just tried another experiment and got an interesting result:

rdmurray@pydev:/python/p34>cat temp1.py import temp2 foo = 1 rdmurray@pydev:/python/p34>cat temp2.py import temp1

badval

rdmurray@pydev:~/python/p34>./python Python 3.4.3+ (3.4:b53cfcfdfe47, May 2 2015, 12:51:46) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information.

import temp2 Traceback (most recent call last): File "", line 1, in File "/home/rdmurray/python/p34/temp2.py", line 3, in badval NameError: name 'badval' is not defined import sys sorted(sys.modules.keys()) ['main', '_codecs', '_collections_abc', '_frozen_importlib', '_imp', '_io', '_sitebuiltins', '_stat', '_sysconfigdata', '_thread', '_warnings', '_weakref', '_weakrefset', 'abc', 'atexit', 'builtins', 'codecs', 'encodings', 'encodings.aliases', 'encodings.latin_1', 'encodings.utf_8', 'errno', 'genericpath', 'io', 'marshal', 'os', 'os.path', 'posix', 'posixpath', 'readline', 'rlcompleter', 'signal', 'site', 'stat', 'sys', 'sysconfig', 'temp1', 'zipimport'] import temp1 temp1.temp2 <module 'temp2' from '/home/rdmurray/python/p34/temp2.py'> sorted(sys.modules.keys()) ['main', '_codecs', '_collections_abc', '_frozen_importlib', '_imp', '_io', '_sitebuiltins', '_stat', '_sysconfigdata', '_thread', '_warnings', '_weakref', '_weakrefset', 'abc', 'atexit', 'builtins', 'codecs', 'encodings', 'encodings.aliases', 'encodings.latin_1', 'encodings.utf_8', 'errno', 'genericpath', 'io', 'marshal', 'os', 'os.path', 'posix', 'posixpath', 'readline', 'rlcompleter', 'signal', 'site', 'stat', 'sys', 'sysconfig', 'temp1', 'zipimport']

More or less the reverse of the passage in question.