Similar can also be achieved using the "imp" module. I have seen this being used in CapTipper. Here's the simple code to load the modules from a directory "modules".
Code:
#!/usr/bin/env python
"""
This trick is inspired/taken from CapTipper - https://github.com/omriher/CapTipper/blob/master/CTPlugin.py
"""
import glob
import os
import imp
module_files = glob.glob("modules/*.py")
for module in module_files:
full_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), module)
print "[+] Importing the module - %s" % full_path
(path, name) = os.path.split(full_path)
(name, ext) = os.path.splitext(name)
(p_file, filename, data) = imp.find_module(name, [path])
try:
mod = imp.load_module(name, p_file, filename, data)
except ImportError:
print "[-] Couldn't import module. Exiting!"
sys.exit(1)
# After the module is loaded, you can run the desired function. For ex.
mod.run()
Using the above trick, you can load the whole module in a variable, lets say "module1" and then it can be used like you would use a module, for ex. module1.function_name( arguments ), module1.variable_name, etc.
Hope this helps. Cheers.
Regards,
c0dist