Loading a class the name of which is only known runtime

Anand anand at calbay.com
Wed Apr 9 01:34:05 CEST 2003


there is a command called exec

you can try something like ...

yourstring = "generated at runtime"

exec("import %s as dynclass" %(yourstring))

Anand.
Mads Orbesen Troest <mads at troest.NEVERMORE.dk> wrote in message news:<13n0q1sc6o6oh$.hfa48a72df4n.dlg at 40tude.net>...
> Hi folks;
> 
> "And now for something completely different:"
> 
> I have a situation in which I, from a CGI script written in Python, want to 
> instantiate a class, the name of which is known to me only at runtime 
> (because it depends on a CGI parameter).
> 
> Is it possible to load (import and instantiate) a class dynamically in 
> Python? I have a feeling the answer is Yes, but as I am unfortunately not 
> quite the python expert (yet :) forgive me if I ask something obvious.
> 
> Thanks in advance,
>    /\/\\ads Orbesen Troest




More information about the Python-list mailing list