Dynamic loading of modules

Johan Fredrik Øhman johanfo at essay.org
Mon Sep 23 23:01:54 CEST 2002


I tried this below.  But as you see, even if the Password.py has a "def init()"
it doesn't work.

Python 2.2 (#1, Mar 26 2002, 15:46:04)
[GCC 2.95.3 20010315 (SuSE)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> z = __import__("plugins.Password")
>>> z.init()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'module' object has no attribute 'init'

--
Johan Fredrik Øhman


"Henrik Motakef" <henrik.motakef at web.de> wrote in message
news:87sn00zeke.fsf at pokey.henrik-motakef.de...
> "Andreas Lydersen" <al at metroic.no> writes:
>
> > Hi,
> > I'd like to load a set of modules from a directory.  I have tried to use the
> > __import__(...) statement to do so, but with no success.
> > ¨
> > The static approch works (of course), but the dynamic does not!
> >
> > static:
> > import plugins.Password
> >
> > dynamic atttempt:
> > __import__("plugins.Password")
>
> __import__ doesn't really import the module, it returns a module
> object. So do something like
>
> >>> pwd = __import__("plugins.Password")
> >>> pwd.spam()
> Spam!
>
> hth
> Henrik





More information about the Python-list mailing list