How to clean python interpreter's environment?

  • Thread starter Rafal Kleger-Rudomin
  • Start date
R

Rafal Kleger-Rudomin

Hello,

I'm looking for a command to reset interpreter's environment i.e. unload
all modules, delete variables etc.

Regards,
Rafal
 
P

Peter Hansen

Rafal said:
I'm looking for a command to reset interpreter's environment i.e. unload
all modules, delete variables etc.

If you're in the interactive interpreter, you should use
the following command: ^Z (on Windows), or ^D (Linux).

(Translation: there's no existing command that does what
you want. Maybe describing your reason for wanting this will
let people suggest alternative solutions.)

-Peter
 
R

Reinhold Birkenfeld

Peter said:
If you're in the interactive interpreter, you should use
the following command: ^Z (on Windows), or ^D (Linux).

(Translation: there's no existing command that does what
you want. Maybe describing your reason for wanting this will
let people suggest alternative solutions.)

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

Reinhold
 
P

Peter Hansen

Reinhold said:
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.

-Peter
 
R

Reinhold Birkenfeld

Peter said:
Reinhold said:
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.

Reinhold
 
R

Rafal Kleger-Rudomin

Reinhold said:
Peter said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,201
Latest member
KourtneyBe

Latest Threads

Top