<div class="gmail_quote">On Mon, Aug 22, 2011 at 12:14 PM, Matthew Brett <span dir="ltr"><<a href="mailto:matthew.brett@gmail.com">matthew.brett@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">Yes, sorry, I should have mentioned that I explored these kind of variations.</div>
<br>
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.<br>
<br>
However, I could imagine the import machinery being able to recognize that 'apkg.subpkg' is broken, and re-import it without error. </blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
Is that reasonable?<br></blockquote><div><br></div><div>Not really, no. Despite the fact that you can sorta do it, and that there's this "reload" function, Python doesn't really support reloading of modules / code. If you want to do it, you basically have to go out and _do_ it yourself. </div>
<div><br></div><div>If you muck about in sys.modules, you need to do so completely. Python and the import stuff won't really do anything to help you (though it won't go out of its way to hinder you, either).</div>
<div><br></div><div>Something like:</div><div><br></div><div>def remove_module(name):</div><div> for mod in sys.modules.keys():</div><div> if mod == name or name.startswith(name + "."):</div><div> del sys.modules[mod]</div>
<div><br></div></div>Then remove_module("apkg") may work for you. (Code above not tested at all, not even kinda)<div><br></div><div>--Ix</div>