I want my code to import all modules from path and allow user to access command
class, but after I trying to import class command
, __import__
fails to do this.
core.py:
def import_modules(path):
modules = dict()
for mod in os.listdir(path):
if mod == '__init__.py' or mod[-3:] != '.py':
continue
else:
m = __import__(mod[:-3]).command() # error here
modules[m.name] = m
return modules
commands = import_modules('test_directory/tests')
commands["test"].run()
test.py:
class command:
def __init__(self):
self.name = "test"
self.description = "test"
self.usage = "Usage: test"
self.args = 1
def run(self):
print("test")
error:
AttributeError: module 'test' has no attribute 'command'
I really need help with this.
I tried via import lib
, via getattr
nothing works. Please help me solve this problem.