Finding a module's sub modules at runtime

Joshua J. Kugler joshua at eeinternet.com
Wed Mar 28 16:41:06 EDT 2007


On Wednesday 28 March 2007 12:04, kyosohma at gmail.com wrote:
>> 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.
> 
> 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

Well, it seems the "help" module is a built in, and has no .py file
anywhere.  The epydoc package uses the imports.file_modules(dirname)
function, which just walks the directory tree.  Thanks for the
pointer...just confirmed that I have to do something I wanted to avoid. 
But I guess if I use Python's os module and the __path__ string, it should
still be nicely portable.  It just seems that since Python is gathering
that information anyway, it should make it available without me having to
walk the directory tree.

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