By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,930 Members | 1,580 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,930 IT Pros & Developers. It's quick & easy.

How to clean python interpreter's environment?

P: n/a
Hello,

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

Regards,
Rafal
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Rafal Kleger-Rudomin wrote:
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
Jul 18 '05 #2

P: n/a
Peter Hansen wrote:
Rafal Kleger-Rudomin wrote:
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.)


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

--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc
Jul 18 '05 #3

P: n/a
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.

-Peter
Jul 18 '05 #4

P: n/a
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.

Reinhold

--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc
Jul 18 '05 #5

P: n/a
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
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.