Error when deleting and reimporting subpackages
matthew.brett at gmail.com
Mon Aug 22 20:51:13 CEST 2011
I recently ran into this behavior:
>>> import sys
>>> import apkg.subpkg
>>> del sys.modules['apkg']
>>> import apkg.subpkg as subpkg
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'subpkg'
where 'apkg' and 'subpkg' comprise empty __init__.py files to simplify the example.
It appears then, that importing a subpackage, then deleting the containing package from sys.modules, orphans the subpackage in an unfixable state.
I ran into this because the nose testing framework does exactly this kind of thing when loading test modules, causing some very confusing errors and failures.
Is this behavior expected?
More information about the Python-list