dynamically load from module import xxx

Guilherme Polo ggpolo at gmail.com
Tue Jul 1 17:22:28 CEST 2008

On Tue, Jul 1, 2008 at 12:11 PM, 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?)

fromlist is used for importing subpackages/submodules of the first arg
of __import__. Since you are using "modulename", I'm guessing it is
not a package, fromlist will do nothing for you.
To solve your problem you could do getattr(__import__('modulename'), 'xxx').

> --
> http://mail.python.org/mailman/listinfo/python-list

-- Guilherme H. Polo Goncalves

More information about the Python-list mailing list