__import__ confusion

Leazen leazen at uol.com.ar
Wed Feb 26 04:50:31 CET 2003

	OK, thanks for clarifying that.
	I'm trying to load modules dinamically. All modules which define a
class (Plugin) should be imported and an object of the class within them
created, do you think using __import__ is the way to go or that I should
keep looking?
	Thanks again, first time doing this kind of thing.


Steven Taschuk wrote:
> Quoth Leazen:
>>	I'm having some trouble understanding how __import__ works.
>>	From the docs I read that "from spam.ham import eggs" results in
>>"__import__('spam.ham', globals(), locals(), ['eggs'])" rendering the
>>later function call 'eggs', what ever that is, but I actually get 'ham'!
> I've just read the blurb on __import__ in section 2.1 of the
> Library Reference, and I don't see anything to indicate that it
> returns the things named in the last argument.  On the contrary:
>     When the name variable is of the form package.module, normally,
>     the top-level package (the name up till the first dot) is
>     returned, not the module named by name. However, when a non-empty
>     fromlist argument is given, the module named by name is returned.
> The last sentence indicates that __import__('spam.ham', globals(),
> locals(), ['eggs']) will return the module named spam.ham, as you
> have found.
>>[...] So I wonder what the last argument is for since it seams rather
>>useless. [...]
> Some implementations of __import__ might find it useful -- it
> might be possible in some circumstances to optimize the import if
> you know precisely which components are needed

More information about the Python-list mailing list