How to instatiate a class of which the name is only known at runtime?

Robert Brewer fumanchu at
Mon Sep 29 21:46:26 CEST 2003

Peter Otten wrote:

> Note that all three versions will not work with submodules, e. g.
> so let's change it one more (last?) time:
> def getClass(classname, modulename):
>     return getattr(__import__(modulename, globals(), locals(),
> classname)

Thanks, Peter, you finally put me on the right track with regards to
importing from packages, with that __import__ builtin. Been fighting
that for a month now, on and off. FWIW, I took your example and expanded
it into a cookbook recipe, replacing my older, much uglier solution:

Robert Brewer
Amor Ministries
fumanchu at

More information about the Python-list mailing list