Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » Python

dynamically load from module import xxx

 
Jump to:  
 
Neal Becker
PostPosted: 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
PostPosted: 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').

Quote:

--
LINK




--
-- Guilherme H. Polo Goncalves
 

 
Neal Becker
PostPosted: 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
PostPosted: 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)

Quote:


--
LINK




--
-- Guilherme H. Polo Goncalves
 

 
Gary Duzan
PostPosted: 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
PostPosted: 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
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

certyfikat energetyczny Ich Szatan - Leśmian Bolesław Lechoń Jan wiersze koszulki poker