开发者_如何学编程Is there any way to import all modules in the current directory, and return a list of them?
For example, for the directory with:
- mod.py
- mod2.py
- mod3.py
It will give you [<module 'mod'>, <module 'mod2'>, <module 'mod3'>]
I think I've got your idea.
Try the following:
import glob
modules = []
for module_name in glob.glob("*.py"):
modules.append(__import__(module_name[:-3]))
This way you get a list of module
objects and don't pollute the global namespace.
精彩评论