On 8/14/06, <b class="gmail_sendername">Dick Moores</b> &lt;<a href="mailto:rdm@rcblue.com">rdm@rcblue.com</a>&gt; wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Actually, my question is, after using IDLE to do some importing of<br>modules and initializing of variables, how to return it to it's<br>initial condition without closing and reopening it.<br><br>So, for example after I've done
<br> &gt;&gt;&gt; import math, psyco<br> &gt;&gt;&gt; a = 4**23<br><br>How can I wipe those out without closing IDLE?<br>(I used to know how, but I've forgotten.)</blockquote><div><br>Hi Dick,<br>&nbsp; Usually this entails removing from the &quot;module registry and removing references to it in the code.&nbsp; If you have a _really_ well used module (like one with config parameters imported into every module), then you'll have an additional step of removing it from every module.&nbsp; Also, if you have use &quot;from psyco import ...&quot;, then you will not be able to free up the module and the reference to the module easily (is it from that module, or imported from a third module? see &quot;if paranoid: code below).
<br><br>The function below deletes a module by name from the Python interpreter, the &quot;paranoid&quot; parameter is a list of variable names to remove from every other module (supposedly being deleted with the module).&nbsp; Be VERY careful with the paranoid param; it could cause problems for your interpreter if your functions and classes are named the same in different modules.&nbsp; One common occurrance of this is &quot;error&quot; for exceptions.&nbsp; A lot of libraries have one &quot;catch-all&quot; exception called &quot;error&quot; in the module.&nbsp; If you also named your exception &quot;error&quot; and decided to include that in the paranoid list... there go a lot of other exception objects.
<br><br>def delete_module(modname, paranoid=None):<br>&nbsp;&nbsp;&nbsp; from sys import modules<br>&nbsp;&nbsp;&nbsp; try:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; thismod = modules[modname]<br>&nbsp;&nbsp;&nbsp; except KeyError:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise ValueError(modname)<br>&nbsp;&nbsp;&nbsp; these_symbols = dir(thismod)
<br>&nbsp;&nbsp;&nbsp; if paranoid:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; paranoid[:]&nbsp; # sequence support<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise ValueError('must supply a finite list for paranoid')<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; these_symbols = paranoid[:]
<br>&nbsp;&nbsp;&nbsp; del modules[modname]<br>&nbsp;&nbsp;&nbsp; for mod in modules.values():<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delattr(mod, modname)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except AttributeError:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pass<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if paranoid:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for symbol in these_symbols:
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if symbol[:2] == '__':&nbsp; # ignore special symbols<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; continue<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delattr(mod, symbol)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except AttributeError:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pass
<br><br>Then you should be able to use this like:<br><br>delete_module('psyco')<br>&nbsp; or<br>delete_module('psyco', ['Psycho', 'KillerError'])&nbsp; # only delete these symbols from every other module (for &quot;from psyco import Psycho, KillerError&quot; statements)
<br></div></div><br>&nbsp; -Arcege<br><br>-- <br>There's so many different worlds,<br>So many different suns.<br>And we have just one world,<br>But we live in different ones.