Skip to content

Instantly share code, notes, and snippets.

@pelletier
Created July 30, 2010 15:24
Show Gist options
  • Save pelletier/500708 to your computer and use it in GitHub Desktop.
Save pelletier/500708 to your computer and use it in GitHub Desktop.
def load_modules(path):
"""
Load all Python modules from a directory into a dict.
:param path: the path to the living place of the modules to load.
:type path: :class:`str`
:returns: map between loaded modules name and their content.
:rtype: :class:`dict`
"""
import os, imp
dir_list = os.listdir(path)
mods = {}
for fname in dir_list:
name, ext = os.path.splitext(fname)
if ext == '.py' and not name == '__init__':
f, filename, descr = imp.find_module(name, [path])
mods[name] = imp.load_module(name, f, filename, descr)
return mods
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment