Getting al classes inside a package

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Dec 17 21:51:57 EST 2007


En Mon, 17 Dec 2007 14:20:38 -0300, Matias Surdi <matiassurdi at gmail.com>  
escribió:

> How can I get all the clasess inside a package (including it  
> subpackages) ?
>
> for example, I have a package with classes A and B and with a subpackage
> wichs has class C.
>
> How can I get a list (and a path) of the classes that exists under the
> root package ?

If the package does not import its modules or subpackages in __init__.py,  
you'll have to inspect the __path__ attribute and enumerate .py files.  
Then, try to load them and find all classes.
Warning: There is no way to tell if a certain module is intended to be  
imported at all or not (might be a test module, an unimplemented feature,  
a leftover from previous versions, a script intended to be run from the  
command line...)
You can use the inspect standard module (mainly getmembers() and  
isclass()) to find all classes; remember to filter by the class __module__  
attribute (else you'll get  imported classes too).

-- 
Gabriel Genellina




More information about the Python-list mailing list