Error when deleting and reimporting subpackages
nagle at animats.com
Tue Aug 23 00:54:58 EDT 2011
On 8/22/2011 11:51 AM, Matthew Brett wrote:
> 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?
It's undefined behavior. You're dealing with CPython implementation
semantics, not Python language semantics.
More information about the Python-list