Finding a module's sub modules at runtime

Joshua J. Kugler joshua at eeinternet.com
Wed Mar 28 15:44:10 EDT 2007


[If this is documented somewhere, please just point me there.  I googled on
the terms that made sense to me, and didn't find anything.]

So, I have:

ModTest
        __init__.py
        AModule.py
        BModule.py
        CModule.py

All works fine.  However, when I import ModTest, I would like it to discover
and store the names of the modules beneath it, and construct a list, say
mod_list, that I can access later to find the names of the sub-modules in
this module.  Kind of setting __all__ at run time, I guess (yes, I'm aware
of the case caveats).

I figured __init__.py coudl take its own __path__ and walk the directory to
find all .py files other than __init__.py, but that seemed hackish.  Is
there an "official" way to do this?  Or a better way?

To give "context:" all the modules will have classes that have the same
name, same methods etc.  One of the modules will be picked depending on
which implementation is needed.

Thanks!

j

-- 
Joshua Kugler
Lead System Admin -- Senior Programmer
http://www.eeinternet.com
PGP Key: http://pgp.mit.edu/  ID 0xDB26D7CE

-- 
Posted via a free Usenet account from http://www.teranews.com




More information about the Python-list mailing list