How to execute code when a module is imported ?

Arthas arthas.thesox at gmail.com
Mon Aug 1 20:05:35 EDT 2005


How I do it?

I wanna execute some code when a module is imported:

-- foo.py --
#!/usr/bin/env python

def codetoexecwhenloaded():
print "bar"
-- end of foo.py --

-- in console --

import foo

^ and then it should print "bar"

Thanks, Arthas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20050802/7917737c/attachment.html>


More information about the Python-list mailing list