473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to clean python interpreter's environment?

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
5 41142
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

46
by: Jon Perez | last post by:
Can one run a 1.5 .pyc file with the 2.x version interpreters and vice versa? How about running a 2.x .pyc using a 2.y interpreter?
0
by: Atul Kshirsagar | last post by:
I am embedding python in my C++ application. I am using Python *2.3.2* with a C++ extention DLL in multi-threaded environment. I am using SWIG-1.3.19 to generate C++ to Python interface. Now to...
13
by: Rolf Magnus | last post by:
Hi, I would like to embed a python interpreter within a program, but since that program would be able to automatically download scripts from the internet, I'd like to run those in a restricted...
38
by: BORT | last post by:
Please forgive me if this is TOO newbie-ish. I am toying with the idea of teaching my ten year old a little about programming. I started my search with something like "best FREE programming...
13
by: dmh2000 | last post by:
I am experimenting with the interactive interpreter environments of Python and Ruby and I ran into what seems to be a fundamental difference. However I may be doing something wrong in Python....
12
by: Tony Belding | last post by:
I'm interested in using an off-the-shelf interpreted language as a user-accessible scripting language for a MUCK. I'm just not sure if I can find one that does everything I need. The MUCK must be...
113
by: John Nagle | last post by:
The major complaint I have about Python is that the packages which connect it to other software components all seem to have serious problems. As long as you don't need to talk to anything outside...
5
by: Adam Atlas | last post by:
Does anyone know if it would be possible to create a CPython extension -- or use the ctypes module -- to access Python's own embedding API (http://docs.python.org/api/initialization.html &c.)?...
4
by: Chris8Boyd | last post by:
I am embedding Python in a MSVC++ (2005) application. The application creates some environment and then launches a Python script that will call some functions exported from the MSVC++ application....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.