dynamically load from module import xxx

Guilherme Polo ggpolo at gmail.com
Wed Jul 2 15:15:14 CEST 2008

On Tue, Jul 1, 2008 at 1:46 PM, Gary Duzan <mgi820 at motorola.com> wrote:
> In article <mailman.1019.1214925114.1044.python-list at python.org>,
> Neal Becker  <ndbecker2 at gmail.com> wrote:
>>What is a good way to emulate:
>>from module import xxx
>>where 'module' is a dynamically generated string?
>>__import__ ('modulename', fromlist=['xxx'])
>>seems to be what I want, but then it seems 'xxx' is not placed in globals()
>>(which makes me wonder, what exactly did fromlist do?)
>   You might want to read what "help(__import__)" tells you. It
> sounds like what you want is:

But have you read it yourself ?

> mynamespace['xxx'] = __import__('modulename.xxx', fromlist=['xxx'])

Only if modulename is actually a package, which doesn't really make
sense for a "modulename". Also, fromlist doesn't need to be ['xxx'] if
you want to import the submodule 'xxx' from the, now supposed,
'modulename' package, it could be anything that is not an empty list.

>   Seems a bit weird to me, but that's the way it is, and I'm sure
> there is a reason for it.
>   Good luck.
>                                        Gary Duzan
>                                        Motorola H&NM
> --
> http://mail.python.org/mailman/listinfo/python-list

-- Guilherme H. Polo Goncalves

More information about the Python-list mailing list