A simpler more pythonic approach to adding in

George Sakkis gsakkis at rutgers.edu
Thu Jan 19 00:34:06 CET 2006


A cleaner, though not shorter, rewriting could be:

from itertools import chain

def ancestors(path):
    while True:
        yield path
        parent = os.path.dirname(path)
        if parent == path:
            break
        path = parent

for dir in chain([os.environ['HOME']],
                  ancestors(os.path.realpath('.'))):
    modules_dir = os.path.join(dir, 'modules')
    if os.path.exists(modules_dir):
        sys.path.insert(0,modules_dir)
        from foo import foo
        break


HTH,
George




More information about the Python-list mailing list