Mailing List Archive

Plugins, or selecting modules to import at runtime
Hi all.

I'd like to write a program in Python in which the user can select one of
several modules to execute through a function that has the same name in all the
modules. I don't believe "import" lets me pass it a string. There is also
reload(), but the module to reload must be previously imported.

This is very similar to plugins like that used in Netscape, Photoshop and the
GIMP.

Can someone please give me a hint?

Thanks.

Please forward replies to gutier@intergate.bc.ca.
Plugins, or selecting modules to import at runtime [ In reply to ]
Never mind. I just found the module "imp".

Thanks.

On Sat, 17 Apr 1999, Gerald Gutierrez wrote:
>Hi all.
>
>I'd like to write a program in Python in which the user can select one of
>several modules to execute through a function that has the same name in all the
>modules. I don't believe "import" lets me pass it a string. There is also
>reload(), but the module to reload must be previously imported.
>
>This is very similar to plugins like that used in Netscape, Photoshop and the
>GIMP.
>
>Can someone please give me a hint?
>
>Thanks.
>
>Please forward replies to gutier@intergate.bc.ca.
Plugins, or selecting modules to import at runtime [ In reply to ]
Gerald Gutierrez <gutier@intergate.bc.ca> writes:
> Never mind. I just found the module "imp".

That's waay overkill for what you need; the builtin function
__import__ will do nicely:

Python 1.5.2 (#2, Apr 14 1999, 13:02:03) \
[GCC egcs-2.91.66 19990314 (egcs-1.1.2 on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> __import__("sys")
<module 'sys' (built-in)>
>>> s=__import__("sys")
>>> s
<module 'sys' (built-in)>
>>>

HTH

Michael

> Thanks.
>
> On Sat, 17 Apr 1999, Gerald Gutierrez wrote:
> >Hi all.
> >
> >I'd like to write a program in Python in which the user can select one of
> >several modules to execute through a function that has the same name in all the
> >modules. I don't believe "import" lets me pass it a string. There is also
> >reload(), but the module to reload must be previously imported.
> >
> >This is very similar to plugins like that used in Netscape, Photoshop and the
> >GIMP.
> >
> >Can someone please give me a hint?
> >
> >Thanks.
> >
> >Please forward replies to gutier@intergate.bc.ca.
Plugins, or selecting modules to import at runtime [ In reply to ]
Gerald Gutierrez <gutier@intergate.bc.ca> wrote:
> I'd like to write a program in Python in which the user can select one of
> several modules to execute through a function that has the same name in all the
> modules. I don't believe "import" lets me pass it a string. There is also
> reload(), but the module to reload must be previously imported.
>
> This is very similar to plugins like that used in Netscape, Photoshop and the
> GIMP.
>
> Can someone please give me a hint?

module = __import__("module")

also see:
http://www.pythonware.com/people/fredrik/fyi/fyi06.htm

</F>