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


Groups > comp.lang.python > #12048

Re: Error when deleting and reimporting subpackages

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <me+list/python@ixokai.io>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:Error': 0.04; 'sys': 0.05; 'attribute': 0.07; 'cached': 0.07; 'subject:when': 0.07; 'python': 0.08; '>>>>': 0.09; 'also:': 0.09; 'behavior:': 0.09; 'content-type:multipart/signed': 0.09; 'filename:fname piece:signature': 0.09; 'am,': 0.12; '"copyright",': 0.16; '"credits"': 0.16; '"license"': 0.16; '(at)': 0.16; '(dot)': 0.16; '[gcc': 0.16; 'content-type:application/pgp-signature': 0.16; 'filename:fname piece:asc': 0.16; 'filename:fname:signature.asc': 0.16; 're-import': 0.16; 'received:209.85.213.174': 0.16; 'received:mail-yx0-f174.google.com': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'example.': 0.18; 'loading': 0.18; 'appears': 0.20; '(most': 0.21; 'blog:': 0.21; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'testing': 0.24; 'traceback': 0.24; 'nov': 0.28; 'framework': 0.28; 'import': 0.28; 'importing': 0.29; 'cc:addr:gmail.com': 0.30; 'brett': 0.30; 'hansen': 0.30; 'modules,': 0.30; 'ran': 0.30; 'yes.': 0.30; 'received:24': 0.32; 'hi,': 0.32; 'does': 0.32; 'to:addr:python-list': 0.33; '...': 0.34; 'causing': 0.34; 'test': 0.34; 'last):': 0.34; 'weird': 0.34; 'object': 0.35; 'file': 0.36; 'doing': 0.36; 'skip:" 10': 0.36; 'some': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'empty': 0.39; 'to:addr:python.org': 0.39; 'where': 0.40; 'more': 0.60; 'kind': 0.61; 'results': 0.61; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.71; 'mail:': 0.73; '11:51': 0.84; '2.7.1': 0.84; 'failures.': 0.84; 'comprise': 0.91; 'url:io': 0.93
Sender Ixokai <ixokai@ixokai.io>
Date Mon, 22 Aug 2011 12:06:44 -0700
From Stephen Hansen <me+list/python@ixokai.io>
MIME-Version 1.0
To python-list@python.org
Subject Re: Error when deleting and reimporting subpackages
References <ac0c2d71-d317-4f14-b389-459a5a6fae29@glegroupsg2000goo.googlegroups.com>
In-Reply-To <ac0c2d71-d317-4f14-b389-459a5a6fae29@glegroupsg2000goo.googlegroups.com>
X-Enigmail-Version 1.1.1
OpenPGP id=555B1FE5; url=http://id.ixokai.io/pub.asc
Content-Type multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enigFCC1AB49AECB3D3279F6CBE3"
Cc Matthew Brett <matthew.brett@gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To me+list/python@ixokai.io
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.327.1314040013.27778.python-list@python.org> (permalink)
Lines 74
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1314040013 news.xs4all.nl 23970 [2001:888:2000:d::a6]:48038
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12048

Show key headers only | View raw


[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/

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web