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?