How to clean python interpreter's environment?

Rafal Kleger-Rudomin r.kleger at chello.nl
Sat Aug 21 06:37:23 EDT 2004


Reinhold Birkenfeld wrote:
> Peter Hansen wrote:
> 
>>Reinhold Birkenfeld wrote:
>>
>>
>>>>Rafal Kleger-Rudomin wrote:
>>>>
>>>>
>>>>>I'm looking for a command to reset interpreter's environment i.e. unload 
>>>>>all modules, delete variables etc.
>>>
>>>What about something like this:
>>>
>>>def clear(keep=("__builtins__", "clear")):
>>>    keeps = {}
>>>    for name, value in globals().iteritems():
>>>        if name in keep: keeps[name] = value
>>>    globals().clear()
>>>    for name, value in keeps.iteritems():
>>>        globals()[name] = value
>>
>>Well, that only removes all the references from the
>>globals of the current module.  Is that all that's
>>wanted?
>>
>>Note that threads that are already running will not be
>>removed, nothing in sys.modules will be removed, and
>>there are doubtless a few other things in the interpreter
>>that aren't quite so easy to get at.
> 
> 
> Right. So I agree with you on ^D.

Well, Control-D just exits python, at least on cygwin. That's not what I 
want.

I should have explained the background of my question:
I write some Python app on Windows using PythonWin IDE. It has own 
Python interpreter window, when I run or debug my app, it runs in that 
interpreter environment. But, after the first run, the environment is 
polluted with modules, vars etc.
What I can do at this moment:
- Exit IDE and run again every time I run/debug my app.
- Run my app in external interpreter (but then I cannot debug with IDE)

Another thing:
Because of this 'cleaning problem', I write all my classes in the main 
file. I should put them in a module. But then I have to manually reload 
the module each time I edit it. Moreover, reload() also does not do the 
job perfectly. So a 'clean' command would be handy.

Do you know any developers' guide addressing such work practices, 
practical tricks, project organisation? I have a book 'Python 2.1 
Bible', it is cool but does not go so far.

Best Regards,
Rafal



More information about the Python-list mailing list