Question

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?

Était-ce utile?

La 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)

Autres conseils

import os

my_dir = '/whatever/directory/'
files = [os.path.join(dirpath, f) for dirpath, dirnames, files in os.walk(my_dir) for f in files if f.endswith('.py')]
modules = [__import__(os.path.splitext(f)[0],globals(),locals(),[],-1) for f in files]
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top