I have a directory structure like this:
|- project
|- commands.py
|- Modules
| |- __init__.py
| |- base.py
| \- build.py
\- etc....
I have the following code in __init__.py
commands = []
hooks = []
def load_modules():
""" dynamically loads commands from the /modules subdirectory """
path = "\\".join(os.path.abspath(__file__).split("\\")[:-1])
modules = [f for f in os.listdir(path) if f.endswith(".py") and f != "__init__.py"]
print modules
for file in modules:
try:
module = __import__(file.split(".")[0])
print module
for obj_name in dir(module):
try:
potential_class = getattr(module, obj_name)
if isinstance(potential_class, Command):
commands.append(potential_class(serverprops))
if isinstance(potential_class, Hook):
hooks.append(potential_class(serverprops))
except:
pass
except ImportError as e:
print "!! Could not load %s: %s" % (file, e)
print commands
print hooks
I am trying to get the __init__.pycorresponding commands and hooks to the specified lists to load, however I always click ImportError on module = __import__(file.split(".")[0]), albeit __init__.pybase.py, etc. all are in the same folder, I confirmed that nothing in any of the module files requires anything in __init__.py, so I really lose what to do.
source
share