[Tutor] Querying a packages modules?

Kent Johnson kent37 at tds.net
Fri Nov 20 13:09:56 CET 2009


On Thu, Nov 19, 2009 at 9:31 PM, Eric Pavey <warpcat at gmail.com> wrote:
> Say I have this package layout
>
> \myPackage
>
> __init__.py
> moduleA.py
> moduleB.py
>
> Is there a way (and I'm sure there is...) to query, for a given package
> level, which modules live under it?
> I thought I could do it like so:
>
> import myPackage
> goodQualityInfo = dir(myPackage)

One way to do this is to include an __all__ attribute in __init__.p]:
__all__ = ['moduleA', 'moduleB']

Then instead of dir(myPackage) use myPackage.__all__.

The name is standard though it is usually used for a slightly different purpose:
http://docs.python.org/tutorial/modules.html#importing-from-a-package

Kent


More information about the Tutor mailing list