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.

Author Calvin.Spealman
Recipients Calvin.Spealman
Date 2011-07-13.19:44:21
SpamBayes Score 8.881784e-16
Marked as misclassified No
Message-id <1310586263.14.0.144683029757.issue12554@psf.upfronthosting.co.za>
In-reply-to
Content
I came across this behavior when it was related to the shadowing of a small typo bug and took me forever to find. In my case, the original error was shadowed in a way that is unrelated to this bug, but lead to the module being imported twice (because it was removed when it failed the first time) and then the second import caused a completely unexpected error, because the state of the submodules conflicted with the import-time logic of the top-level package.

I think when a module fails to load, all of its sub-modules should be removed from sys.modules, not just itself.

calvin@willow-delta:/tmp$ mkdir foo/
calvin@willow-delta:/tmp$ cat >> foo/__init__.py
import foo.bar
1/0
calvin@willow-delta:/tmp$ cat >> foo/bar.py
name = "bar"
calvin@willow-delta:/tmp$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "foo/__init__.py", line 2, in <module>
    1/0
ZeroDivisionError: integer division or modulo by zero
>>> import sys
>>> sys.modules['foo.bar']
<module 'foo.bar' from 'foo/bar.py'>
>>>
History
Date User Action Args
2011-07-13 19:44:23Calvin.Spealmansetrecipients: + Calvin.Spealman
2011-07-13 19:44:23Calvin.Spealmansetmessageid: <1310586263.14.0.144683029757.issue12554@psf.upfronthosting.co.za>
2011-07-13 19:44:22Calvin.Spealmanlinkissue12554 messages
2011-07-13 19:44:22Calvin.Spealmancreate