dynamic includes from classes...

mike mhouck at eecs.tufts.edu
Thu Sep 12 12:48:53 EDT 2002

Okay two questions...

First, is there a better way than exec to dynamically include a module?
importvar = "foo"
exec("from %s import %s" % (importvar, importvar))

And, if there isn't, I tried the above code in a class that I wrote.  But,
it seems that the imported class foo is only visible inside the class, if I
send importvar to another function and do

a = eval(importvar)(args) #btw is there a better way to do this too?

it gives the error "Foo" undeclared.

Any suggestions on how to do this whole thing better, or how to make the
exec include global?


More information about the Python-list mailing list