I'm developing a Python 2.6 package in which I would like to fetch a list of all classes in a certain directory (within the package) in order to then perform introspection on the class objects.
Specifically, if the directory containing the currently executing module has a sub-dir called 'foobar' and 'foobar' contains .py files specifying
Modules are never loaded automatically, but it should be easy to iterate over the modules in the directory and load them with the
__import__ builtin function:
import os import glob for file in glob(os.path.join(os.path.dirname(os.path.abspath(__file__))), "*.py"): name = os.path.splitext(os.path.basename(file)) # add package prefix to name, if required module = __import__(name) for member in dir(module): # do something with the member named ``member``