[Tutor] Querying a packages modules?

Eric Pavey warpcat at gmail.com
Fri Nov 20 17:07:13 CET 2009


On Fri, Nov 20, 2009 at 4:09 AM, Kent Johnson <kent37 at tds.net> wrote:

> 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
>

Thanks.  I was considering that too, but I want to be able to have people
drop modules in that dir and "be done with it":  Not also have them also
need to update __all__ in __init__.py
Appreciate the suggestion though.
My current hacky plan is just query the location on disk of the imported
package, then do a dir search for .py files in that dir, and process those.
Seems clunky though.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091120/f2705eab/attachment.htm>


More information about the Tutor mailing list