Error when deleting and reimporting subpackages
Stephen Hansen
me+list/python at ixokai.io
Mon Aug 22 15:06:44 EDT 2011
On 8/22/11 11:51 AM, Matthew Brett wrote:
> Hi,
>
> 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?
Yes. Doing an import of "apkg.subpkg" results in more then just "test1"
being cached in sys.modules, and you're removing half of that so leaving
Python in a weird state.
You also want to del sys.modules["apkg.subpkg"], then you'll be able to
re-import apkg.subpkg. I.e:
Python 2.7.1 (r271:86882M, Nov 30 2010, 10:35:34)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import test1.test2
>>> del sys.modules['test1']
>>> del sys.modules['test1.test2']
>>> import test1.test2 as test2
>>>
--
Stephen Hansen
... Also: Ixokai
... Mail: me+list/python (AT) ixokai (DOT) io
... Blog: http://meh.ixokai.io/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 487 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20110822/971df111/attachment-0001.sig>
More information about the Python-list
mailing list