Here's how to find all the modules in some directory, and import them.
Finding Functions Within a Module
So, putting them together,...
Finding Modules in a Directory
Is there a better way than just listing the contents of the directory, and taking those tiles that end with ".pyc" or ".py"..?
But perhaps there isn't.
1 import os
2 import sets
3
4 def find_modules(path="."):
5 """Return names of modules in a directory.
6
7 Returns module names in a list. Filenames that end in ".py" or
8 ".pyc" are considered to be modules. The extension is not included
9 in the returned list.
10 """
11 modules = sets.Set()
12 for filename in os.listdir(path):
13 module = None
14 if filename.endswith(".py"):
15 module = filename[:-3]
16 elif filename.endswith(".pyc"):
17 module = filename[:-4]
18 if module is not None:
19 s.add(module)
20 return list(modules)
Importing the Modules
How do you import a module, once you have it's name?
With the [http://www.python.org/doc/current/lib/module-imp.html the imp module!] It dynamically loads named modules.
Finding the Things Inside a Module
Once you have your module, you can look inside it, with .__dict__.
1 module.__dict__
Identifying Functions
If you want to identify, say, functions within the module, you can write code similar to so:
Discussion
- (none yet!)