Error when deleting and reimporting subpackages

Matthew Brett matthew.brett at
Mon Aug 22 14:51:13 EDT 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 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 mailing list