Finding a module's sub modules at runtime

kyosohma at gmail.com kyosohma at gmail.com
Wed Mar 28 22:04:25 CEST 2007


On Mar 28, 2:44 pm, "Joshua J. Kugler" <jos... at eeinternet.com> wrote:
> [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 Programmerhttp://www.eeinternet.com
> PGP Key:http://pgp.mit.edu/ ID 0xDB26D7CE
>
> --
> Posted via a free Usenet account fromhttp://www.teranews.com

I think you need to research how to create documentation. When I
import a module/package, I can then type help(moduleName) and it'll
give me the module or package's contents.

http://mail.python.org/pipermail/python-list/2003-February/192069.html

You may be able to figure out how to do this just be studying the
"help" module itself.

Mike




More information about the Python-list mailing list