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

Wed Sep 10 21:46:20 CEST 2003

On Tue, Sep 09, 2003 at 04:16:45PM -0400, John Roth wrote:
> "Marco Herrn" <herrn at> wrote in message
> news:mailman.1063135996.15871.python-list at
> > Currently it is planned that all these classes reside in a specific
> > subdirectory. Is it possible to say 'import * from subdirectory' ?
> No. It is possible to load the names of all the modules in the
> subdirectory, though. That doesn't tell you what classes are in
> each module unless you either establish some standards or use
> a configuration file of some kind.

That is ok to me. I know the name of the subdirectory and the name of
the class can be found in a configfile. The class then has to be in a
file with the same name as the class, but that is ok. I already planned
establishing a certain naming convention.
> You could, of course, simply import all of the modules, and then
> loop through them looking for the classes.

How would this be done? Would I have to search the directory for files
and load each of them with __import__("subdir/filename")?
Every morning, I get up and look through the 'Forbes' list of the
richest people in America.  If I'm not there, I go to work

Marco Herrn             herrn at
(GnuPG/PGP-signed and crypted mail preferred)
Key ID: 0x94620736

More information about the Python-list mailing list