package introspection...

Skip Montanaro skip at
Mon Feb 24 21:38:23 CET 2003

I frequently poke around in modules using dir() when I can't quite remember
what I'm looking for.  This technique generally fails for packages because
they generally don't import their subsidiary modules unless asked for:

>>> import spambayes
>>> dir(spambayes)
['__builtins__', '__doc__', '__file__', '__name__', '__path__', '__version__']

The best I've come up with so far is

>>> [os.path.splitext(p)[0] for p in os.listdir(os.path.dirname(spambayes.__file__)) if p.endswith(".py")]
['__init__', 'cdb', 'cdb_classifier', 'chi2', 'classifier', 'compatheapq',
'compatsets', 'Corpus', 'CostCounter', 'dbmstorage', 'Dibbler',
'FileCorpus', 'hammie', 'hammiebulk', 'Histogram', 'mboxutils', 'msgs',
'optimize', 'OptionConfig', 'Options', 'PyMeldLite', 'storage',
'TestDriver', 'Tester', 'tokenizer'] 

which I will add to my personal toolkit as something like

    def pkgdir(mod):
        contents = dir(mod)
        if hasattr(mod, "__file__"):
            contents += ["[%s]"%os.path.splitext(p)[0]
                          for p in os.listdir(os.path.dirname(mod.__file__))
                            if (p.endswith(".py") or p.endswith(".so")) and
                                os.path.splitext(p)[0] not in contents and
                                p != ""]
        return contents

if someone doesn't have a better suggestion.  It generates what to me seem
like reasonable results:

>>> pkgdir(email)
['Charset', 'Encoders', 'Errors', 'Generator', 'Header', 'Iterators',
'MIMEAudio', 'MIMEBase', 'MIMEImage', 'MIMEMessage', 'MIMEMultipart',
'MIMENonMultipart', 'MIMEText', 'Message', 'Parser', 'Utils', '[_compat21]',
'[_compat22]', '[_parseaddr]', 'base64MIME', 'message_from_file',
'message_from_string', 'quopriMIME'] 
>>> pkgdir(spambayes)
['[Corpus]', '[CostCounter]', '[Dibbler]', '[FileCorpus]', '[Histogram]',
'[OptionConfig]', '[Options]', '[PyMeldLite]', '[TestDriver]', '[Tester]',
'[cdb]', '[cdb_classifier]', '[chi2]', '[classifier]', '[compatheapq]',
'[compatsets]', '[dbmstorage]', '[hammie]', '[hammiebulk]', '[mboxutils]',
'[msgs]', '[optimize]', '[storage]', '[tokenizer]', '__builtins__',
'__doc__', '__file__', '__name__', '__path__', '__version__'] 



More information about the Python-list mailing list