W
Walter Brunswick
What is the best way to import all modules in a directory (and possibly a subdirectory/subdirectories), possibly including
conditionals, such as regexes?
conditionals, such as regexes?
Walter said:What is the best way to import all modules in a directory
> (and possibly a subdirectory/subdirectories), possibly including
conditionals, such as regexes?
Sam said:On the second point, a combination of sys.path, os.listdir and
__import__ should do what you're after, although sifting through the
whole of sys.path and subfolders from Python, rather than the
interpreter itself, could be slow. (And it'll be redundant as well -
__import__ will have do the same thing, though you could fix that by
using the imp module).
-Should- work, but not tested, so don't blame me if it doesn't:
__import__(modulename) is not equivalent to import modulename;
__import__ returns a module object
[QUOTE][/QUOTE]
Walter said:The purpose is rather irrelevant.
The purpose of something is not only relevant but essential when someone
asks for the "best" way to do it.
For example, without knowing anything of your purpose, I could simply
say that writing a module with a series of import statements, one per
module in the target subdirectory, is the "best" way, and I could easily
defend that as "best" against all other possible approaches until we
knew what the real reason for doing this was.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.