Search code examples
pythondjangoreflectionintrospection

Python - Import all files in a directory tree


I want to import all python files in a directory tree, i.e. if we have the following directory structure:

tests/
tests/foo.py
tests/subtests/bar.py

(Imagine that the tree is of arbitrary depth).

I would like to do import_all('tests') and load foo.py and bar.py. Importing with the usual modules names (tests.foo and tests.subtests.bar) would be nice, but is not required.

My actual use case is that I have a whole bunch of code containing django forms; I want to identify which forms use a particular field class. My plan for the above code is to load all of my code, and then examine all loaded classes to find form classes.

What's a nice, simple way to go about this in python 2.7?


Solution

  • Here's a rough and ready version using os.walk:

    import os
    prefix = 'tests/unit'
    for dirpath, dirnames, filenames in os.walk(prefix):
        trimmedmods = [f[:f.find('.py')] for f in filenames if not f.startswith('__') and f.find('.py') > 0]
        for m in trimmedmods: 
            mod = dirpath.replace('/','.')+'.'+m
            print mod
            __import__(mod)