load a class dynamically

Fernando Pérez fperez528 at yahoo.com
Fri Nov 30 02:04:47 CET 2001


Jeremy Lowery wrote:

> Now I'm in search of finding a way to make a dynamic modules,
> packages to add the classes to. I'm writing an app that stores all
> of this stuff in a ZODB and lets users write code on a server over a
> network. And I'm trying to figure out a way to dynamically "pack"
> all of the class definitions into modules and packages so that I can
> plug the base package into the namespace before the code is run.
> 
> like one user importing Users.JLowery.ACoolModule
> in the code that is going to be piped over the network, and then the
> module/package structure is dynamically created from the objects in
> the ZODB. (Like the User package gets put in the global namespace)
> (Of course, not dynamically recreate it on every request but you get
> the idea.)
> 
> Jeremy
> 
> 
> 

Never mind the funny variable names, but this does exactly (I think) 
that:

    for _t.mod in __IP.rc.import_mod:
        try:
            exec 'import '+_t.mod in locals()
        except :
            __IP.InteractiveTB()
            import_fail_info(_t.mod)

__IP.rc.import_mod is a list of strings with names of modules which 
get loaded into the user namespace after the program is running.

Does this help?

f



More information about the Python-list mailing list