Dyanmic import of a class

Arnaud Delobelle arnodel at googlemail.com
Thu Mar 8 17:01:43 EST 2007


On Mar 8, 9:09 pm, "rh0dium" <steven.kl... at gmail.com> wrote:
[snip]
> for mod in listdir():
>    __import__(mod)
>    a=mod()
>    a.dosomething()  # This is a function which each class shares.
>
> Can anyone help?

You are not using __import__ correctly.  Perhaps reading the doc would
be a good start:
http://docs.python.org/lib/built-in-funcs.html

For example to import the module defined in 'foo.py' you would do
foo = __import__('foo')
Then your class foo would be accessible as foo.foo

HTH

--
Arnaud




More information about the Python-list mailing list