How to: get list of modules in a package

Mike C. Fletcher mcfletch at rogers.com
Tue Oct 12 11:29:57 EDT 2004


Peter L Hansen wrote:
...

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

I used to do this somewhere in mcf.vrml (old VRML97 processing 
package).  The idea was to generate a module to hold classes which were 
also generated on-the-fly (from VRML97 prototypes).  You could then 
import the classes from the non-existent module as long as the module 
itself (which roughly corresponded to a VRML97 file) was part of your 
pickle.  The package holding those modules was empty until such time as 
you either generated one of those modules or loaded one from a pickle, 
and there was just no reasonable way to know what would eventually show 
up there.

Advances in Python (and my moving away from my perverse love of such 
tricks ;) ) made that particular usage obsolete, but it definitely has 
been done in the past...

Enjoy,
Mike

________________________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://www.vrplumber.com
  http://blog.vrplumber.com




More information about the Python-list mailing list