How do you register cleanup code to be run after script execution?

Miki Tebeka miki.tebeka at zoran.com
Mon May 10 02:52:28 EDT 2004


Hello,

> I'm writing a module and when it is imported by a script I want some code
> automatically executed when the importing script is finished executing.  I'd
> like it to execute before interactive mode is entered when executing the
> importing script from the command line.  I don't want to have to impose that
> the importing script must call a function at it's end.
If I understand you correctly than you can just add a function call at 
the end of the module. This function will be executed once when the 
module is imported.

--- hello.py --
a = 1
b = 2
def hello():
     print "Hello"
c = 3

hello()
--- hello.py ---
 >>> import hello
hello
 >>>


HTH.
Miki



More information about the Python-list mailing list