Running code on module import

John Roth newsgroups at jhrothjr.com
Sat May 22 12:45:18 EDT 2004


"Grant D. Watson" <vbasicboy at aol.com> wrote in message
news:20040521050534.21796.00001788 at mb-m15.aol.com...
> If this has been answered before, or if my terminology is off, please bear
with
> me; my Python experience is limited to use in one class and to personal
> projects.
>
> I'd like to do something rather silly:  I'd like to run a particular piece
of
> code from a given module every time that the module is imported, and not
just
> at the time that the module is originally loaded.  So, every time a module
says
>     import foo
> or something analogous, I want foo.doYourThing() to be invoked, in
addition to
> the usual effects on the importing module's namespace.

Look at the built-in __import__() function, and the ihooks module.
For what you want to do, you should be able to hook __import__
so that you get control both before and after it.

John Roth

> Grant D. Watson
> grant_watson at yahoo.pleasedontspamme.com (Use this one!)





More information about the Python-list mailing list