[Idle-dev] Clearing the environment

Mats Wichmann mats@laplaza.org
Tue, 07 Aug 2001 10:05:33 -0600


This came up on comp.lang.python, and it seems like it's
been visited here as well (I seem to have deleted the
messages, if so, so maybe my memory is just failing):

============ begin clip ======================
On 6 Aug 2001 15:09:07 -0700, Ben C <benc1@today.com.au> wrote:
 >Hi,
 >
 >Sorry to bother the group with such a trivial Q but for some reason I
 >can not find an answer to this in the docs ... how does one 'clear'
 >the Python environment .... so all loaded modules and variables are
 >freed? (ie return the Python environment back to its initial state at
 >start up) I assumed it would have been in the sys module?

The command is actually in the os module:

os.execvp('python', ('python',))


It would be difficult to reset the system "live".  You'd have to unload all
the modules (remove them out of the namespace, and also remove their
sys.modules entries).  Then you'd have to clear out the rest of the namespace,
except things like __builtins__ and None (you want to keep __builtins__ so you
don't lose access to __import__), and manually import the modules the toplevel
automatically imports when it starts (such as site and exceptions (well, not
exceptions any more, I guess)).  And probably a bunch of other things I
forgot.

============ end clip ======================

Speaking for me now,

I can see situations where I'd like to have the environment
in the "shell" reset, but the command history remembered,
so just wiping out the shell window before rerunning the
scipt is not ideal.  Like, if I've tried out some manual
inspection commands that showed a problem in the module
under development; I'd like to rerun in a clean environment
yet be able to call those commands back.  Reload is only
sometimes the answer...

Is this a silly desire?  /Is/ there a reasonable way to
accomplish this?

Mats