|  | dynamically load from module import xxx |  | |
| | | Neal Becker |  |
| Posted: Tue Jul 01, 2008 1:11 pm Post subject: dynamically load from module import xxx |  |
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?) |
| |
| | | Guilherme Polo |  |
| Posted: Tue Jul 01, 2008 1:22 pm Post subject: Re: dynamically load from module import xxx |  |
On Tue, Jul 1, 2008 at 12:11 PM, Neal Becker <ndbecker2@gmail.com> wrote:
| Quote: | 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').
-- -- Guilherme H. Polo Goncalves |
| |
| | | Neal Becker |  |
| Posted: Tue Jul 01, 2008 1:55 pm Post subject: Re: dynamically load from module import xxx |  |
Guilherme Polo wrote:
| Quote: | On Tue, Jul 1, 2008 at 12:11 PM, Neal Becker <ndbecker2@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').
|
This seems to be what I want, don't know if there is a simpler way:
stuff =['A','B'] module = __import__ (modulename) for e in stuff: globals().update({e : module.__dict__[e]}) |
| |
| | | Guilherme Polo |  |
| Posted: Tue Jul 01, 2008 2:39 pm Post subject: Re: dynamically load from module import xxx |  |
| |  | |
On Tue, Jul 1, 2008 at 12:55 PM, Neal Becker <ndbecker2@gmail.com> wrote:
| Quote: | Guilherme Polo wrote:
On Tue, Jul 1, 2008 at 12:11 PM, Neal Becker <ndbecker2@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').
This seems to be what I want, don't know if there is a simpler way:
stuff =['A','B'] module = __import__ (modulename) for e in stuff: globals().update({e : module.__dict__[e]})
|
You could change that line to: globals()[e] = getattr(module, e)
-- -- Guilherme H. Polo Goncalves |
| |
| | | Gary Duzan |  |
| Posted: Tue Jul 01, 2008 4:46 pm Post subject: Re: dynamically load from module import xxx |  |
In article <mailman.1019.1214925114.1044.python-list@python.org>, Neal Becker <ndbecker2@gmail.com> wrote:
| Quote: | 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:
mynamespace['xxx'] = __import__('modulename.xxx', fromlist=['xxx'])
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 |
| |
| | | Guilherme Polo |  |
| Posted: Wed Jul 02, 2008 11:15 am Post subject: Re: dynamically load from module import xxx |  |
| |  | |
On Tue, Jul 1, 2008 at 1:46 PM, Gary Duzan <mgi820@motorola.com> wrote:
| Quote: | In article <mailman.1019.1214925114.1044.python-list@python.org>, Neal Becker <ndbecker2@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 ?
| Quote: | 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.
| Quote: | 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
-- LINK
|
-- -- Guilherme H. Polo Goncalves |
| |
|
|