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'>]
Is there any way to import all modules in the current directory, and return a list of them?
For example, for the directory with:
It will give you [<module 'mod'>, <module 'mod2'>, <module 'mod3'>]
I hope this is what you mean
import os
listOfMods = []
for i in os.listdir(pathToDirWithModules):
if i[-3:] == '.py':
modname = i[i.rfind('/'):-3] # '/' for windows '\\' for linux
eval("import " +modname)
listOfMods.append(modname)
return listOfMods
this should work
import glob
foo=glob.glob("*.py")
print foo #prints the list of py files
# to import the modules,this should work
for i foo:
eval("import " + i)