Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12047 > unrolled thread

Error when deleting and reimporting subpackages

Started byMatthew Brett <matthew.brett@gmail.com>
First post2011-08-22 11:51 -0700
Last post2011-08-22 21:54 -0700
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Error when deleting and reimporting subpackages Matthew Brett <matthew.brett@gmail.com> - 2011-08-22 11:51 -0700
    Re: Error when deleting and reimporting subpackages Stephen Hansen <me+list/python@ixokai.io> - 2011-08-22 12:06 -0700
      Re: Error when deleting and reimporting subpackages Matthew Brett <matthew.brett@gmail.com> - 2011-08-22 12:14 -0700
      Re: Error when deleting and reimporting subpackages Matthew Brett <matthew.brett@gmail.com> - 2011-08-22 12:14 -0700
    Re: Error when deleting and reimporting subpackages John Nagle <nagle@animats.com> - 2011-08-22 21:54 -0700

#12047 — Error when deleting and reimporting subpackages

FromMatthew Brett <matthew.brett@gmail.com>
Date2011-08-22 11:51 -0700
SubjectError when deleting and reimporting subpackages
Message-ID<ac0c2d71-d317-4f14-b389-459a5a6fae29@glegroupsg2000goo.googlegroups.com>
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?

Best,

Matthew

[toc] | [next] | [standalone]


#12048

FromStephen Hansen <me+list/python@ixokai.io>
Date2011-08-22 12:06 -0700
Message-ID<mailman.327.1314040013.27778.python-list@python.org>
In reply to#12047

[Multipart message — attachments visible in raw view] — view raw

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/

[toc] | [prev] | [next] | [standalone]


#12050

FromMatthew Brett <matthew.brett@gmail.com>
Date2011-08-22 12:14 -0700
Message-ID<c2b69c5f-566a-4ed1-a1f2-4ddbf940e9c8@glegroupsg2000goo.googlegroups.com>
In reply to#12048
On Monday, August 22, 2011 12:06:44 PM UTC-7, Stephen Hansen wrote:
> 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
> >>>

Yes, sorry, I should have mentioned that I explored these kind of variations.

I think I see that there isn't an obvious way for del sys.modules['apkg'] to know to delete or modify 'apkg.subpkg', because sys.modules is just a dict.

However, I could imagine the import machinery being able to recognize that 'apkg.subpkg' is broken, and re-import it without error.

Is that reasonable?

Best,

Matthew

[toc] | [prev] | [next] | [standalone]


#12051

FromMatthew Brett <matthew.brett@gmail.com>
Date2011-08-22 12:14 -0700
Message-ID<mailman.328.1314040499.27778.python-list@python.org>
In reply to#12048
On Monday, August 22, 2011 12:06:44 PM UTC-7, Stephen Hansen wrote:
> 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
> >>>

Yes, sorry, I should have mentioned that I explored these kind of variations.

I think I see that there isn't an obvious way for del sys.modules['apkg'] to know to delete or modify 'apkg.subpkg', because sys.modules is just a dict.

However, I could imagine the import machinery being able to recognize that 'apkg.subpkg' is broken, and re-import it without error.

Is that reasonable?

Best,

Matthew

[toc] | [prev] | [next] | [standalone]


#12061

FromJohn Nagle <nagle@animats.com>
Date2011-08-22 21:54 -0700
Message-ID<4e5332c7$0$2177$742ec2ed@news.sonic.net>
In reply to#12047
On 8/22/2011 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?

    It's undefined behavior.  You're dealing with CPython implementation
semantics, not Python language semantics.

				John Nagle

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web