How to: get list of modules in a package

Dan Perl danperl at
Tue Oct 12 19:33:44 CEST 2004

Thanks, Peter.  So the fact I didn't see any solution was not due to my lack 
of knowledge.  I thought I should ask and maybe someone would point out an 
elegant solution that was just beyond my own grasp of Python.

os.listdir() would not work in the general case, but it's good enough in my 
limited case.  The package is well-determined and it's always a directory. 
Even if I will change that some day, there will always be another equivalent 
for the os.listdir.

So thanks again, I can confidently go and implement a solution based on 


"Peter L Hansen" <peter at> wrote in message 
news:dfWdnaXpKtgvb_bcRVn-tg at
> Dan Perl wrote:
>> Is there a direct way to get a list of all the modules in a package, 
>> other than using an os.listdir() for the directory of the package?
> Given the dynamic nature of Python, I doubt that's easy to do.
> I'm not sure even os.listdir(), for example, can be relied on,
> given that you can import modules from zip files, or if the
> import hook is used, even from remote locations or, presumably
> (though I haven't seen it done yet), from "thin air" where
> the module is generated dynamically.
> -Peter 

More information about the Python-list mailing list