Clearing The Interactive Python Environment

Quinn Dunkan quinn at retch.ugcs.caltech.edu
Mon Aug 6 20:16:12 EDT 2001


On 6 Aug 2001 15:09:07 -0700, Ben C <benc1 at 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.



More information about the Python-list mailing list