Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Dynamic importing

Reply
Thread Tools

Re: Dynamic importing

 
 
Peter Abel
Guest
Posts: n/a
 
      06-23-2003
(Roy S. Rapoport) wrote in message news:<. com>...
Ok it took me a while, and I'm not quite sure, but I think I got it:
....
....
....

> However, lets look at the package approach:
> onec.py and twoc.py remain the same. master.py looks slightly
> different:
> class master:
> def dosomething(self, what):
>
> x = __import__("demo."+what)


When what='onec' then after the above statement x will become the **demo**
- package with the possibility to access demo.onec.

> y = eval("x.%s()" % what)


So the above eval-statement tries to call demo.onec(), which results in
your error below.
Instead of this you should try:
**y = eval("x.%s.%s()" % (what,what) )**

> eval("y.dosomething()")


BTW at my opinion there's no need for the eval-statement.
**y.dosomething()** works too.

>
>
> (so the only difference is in that import statement).
>
> Running the test code:
> import demo.master
> F = demo.master.master()
> F.dosomething('one')
>
> Gets me
> ---
> Traceback (most recent call last):
> File "./f2", line 6, in ?
> F.dosomething('one')
> File "./demo/master.py", line 5, in dosomething
> y = eval("x.%s()" % what)
> File "<string>", line 0, in ?
> TypeError: 'module' object is not callable
> ---
>
> So there's some intricacy in dealing with packages I'm not getting;
> unfortunately, the only place I've seen packages documented is the
> tutorial which is, understandably, a little sparse.
>
> -roy


I added some prints in your master-class to clearify a little bit
the situation.
class master:
def dosomething(self, what):
x = __import__("demo."+what)
print "="*30
print "x : %s"%x
print "x.__name__: %s"%x.__name__
print "dir(x) :"
print "\n".join(dir(x))
mod="x.%s.%s()" % (what,what)
print "mod : %s"%mod
y = eval(mod)
y.dosomething()
print "="*30
print

Gives me the following:
==============================
x : <module 'demo' from 'C:\Dokumen ...[snip]
x.__name__: demo
dir(x) :
__all__
__builtins__
__doc__
__file__
__name__
__path__
master
onec
mod : x.onec.onec()
class one called to do something
==============================

==============================
x : <module 'demo' from 'C:\Dokumen ...[snip]
x.__name__: demo
dir(x) :
__all__
__builtins__
__doc__
__file__
__name__
__path__
master
onec
twoc
mod : x.twoc.twoc()
class two called to do something
==============================

Reading http://www.python.org/doc/essays/packages.html could
help a bit.

Regards
Peter
 
Reply With Quote
 
 
 
 
Roy S. Rapoport
Guest
Posts: n/a
 
      06-24-2003
p- (Peter Abel) wrote in message news:< om>...

> > y = eval("x.%s()" % what)

>
> So the above eval-statement tries to call demo.onec(), which results in
> your error below.
> Instead of this you should try:
> **y = eval("x.%s.%s()" % (what,what) )**


Alrighty, then. Now we're cooking with gas, and this does in fact
work.
I'm not sure I have a very strong understanding yet of why it works
(whereas the original didn't), but I'm hoping this:
> Reading http://www.python.org/doc/essays/packages.html could


will in fact clarify things.

Thanks!

-roy
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problems importing excel workbook and displaying in dynamic datagrid optimizeit@cox.net ASP .Net 0 02-24-2005 05:46 PM
How to keep a module with the same name as a module it is importing from importing itself? plb Python 2 02-08-2005 03:14 PM
Dynamic module importing with McMillan installer Sion Arrowsmith Python 1 06-16-2004 04:49 PM
importing embedded dynamic lib Vio Python 1 06-10-2004 07:20 AM
Does Pix or cisco router support dynamic-to-dynamic IPSec VPN? c Cisco 2 01-13-2004 01:53 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57