473,656 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 41229
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().iteri tems():
if name in keep: keeps[name] = value
globals().clear ()
for name, value in keeps.iteritems ():
globals()[name] = value

Reinhold

--
Wenn eine Linuxdistributi on 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().iteri tems():
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().iteri tems():
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 Linuxdistributi on 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().iteri tems():
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
6266
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
1839
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 explain it in details, 1. Python initialization and finalization is done in the *main* thread. 2. For each new thread I create a separate sub-interpreter . 3. Using PyRun_String("import myModule"...) before execution of python
13
4011
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 environment, which basically means that I want to allow only a specific set of modules to be used by the scripts, so that it wouldn't be possible for them to remove files from the hard drive, kill processes or do other nasty stuff. Is there any...
38
4789
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 language for kids." After MUCH clicking and high-level scanning, I am looking at Python and Forth. Both have advocates that say each is a great approach to learning computers. My programming classes were a long, long time ago in a land far, far...
13
3346
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. Please comment and correct me if I am wrong In both languages, you can start up the interactive interpreter ('python' and 'irb'), load source files and do stuff, like create objects and call their methods. When you want to change something, you can...
12
2940
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 able to call the interpreter and execute scripts with it, but the interpreter must also be able to call functions in the MUCK code. And then there's the security issue that really worries me. . . I have to be able to limit what the interpreter...
113
5254
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 the Python world, you're fine. But once you do, things go downhill. MySQLdb has version and platform compatibility problems. So does M2Crypto. The built-in SSL support is weak. Even basic sockets don't quite work right; the socket module...
5
1638
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.)? Could a Python program itself create a sub-interpreter, and work with it with all the privileges and capabilities that an actual C program would have? I realize that this may be a bit too... mystical? ... for a lot of people's tastes, but I'm just...
4
2824
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. I want to be able to debug the Python script by using a debug server, like Winpdb (winpdb.org). I use ActivePython 2.5.2.2, Microsoft Visual Studio 2005, and Winpdb 1.3.8.
0
8382
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8297
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8717
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8498
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8600
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7311
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1600
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.