The distribute module provides a mechanism that does much of this. First, you might start by listing the python files in a package using pkg_resources.resource_listdir:
>>> module_names = set(os.path.splitext(r)[0] ... for r ... in pkg_resources.resource_listdir("sqlalchemy", "/") ... if os.path.splitext(r)[1] in ('.py', '.pyc', '.pyo', '') ... ) - set(('__init__',)) >>> module_names set(['engine', 'util', 'exc', 'pool', 'processors', 'interfaces', 'databases', 'ext', 'topological', 'queue', 'test', 'connectors', 'orm', 'log', 'dialects', 'sql', 'types', 'schema'])
You could then import each module in a loop:
modules = {} for module in module_names: modules[module] = __import__('.'.join('sqlalchemy', module))
from package import *anddir(package)insufficient? Note thatdiris a Python command, as well as a command line one.['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']without any mention of the modules inpackage.dirshows the modules if the modules themselves are explicitly imported, though, but that defeats the purpose of the question.__init__.pyfile in the folder? Even empty, this is what tells python to treat the folder as a module.__init__.py. What I didn't have is what @MarkHildreth helpfully referenced: the line__all__ = ['bar','baz']in__init__.py.