I think that you are passing the arguments like this:
python app.py update attribute
so your program interprets it like this:
getattr(__import__('modules.update.update'),command)()
as you don't have a module with that name you get that import error.
If I understood correctly you have the following files structure:
app.py
modules
|
-- __init__.py
|
-- update.py
Edit:
let me suppose for the sake of explaining, that your update.py
contains:
class update(object):
name = 'update_name'
now in your app.py
:
import sys
arguments = sys.argv
module = arguments[1]
command = arguments[2]
moduleObj = __import__(module) # returns module object "modules"
submodule = getattr(moduleObj, module) # returns the module object "modules.update"
moduleClass = getattr(submodule, module) # returns the class object "update"
# this works because the class has the same name as the file it contains it.
# if the names are different then you have to change that accordingly
name_attr = getattr(moduleClass, command) # returns the attribute with the name =command
to see what you got add some prints:
print moduleObj
print submodule
print moduleClass
print name_attr
you can call your script now with:
python app.py update name
and the output will look like:
<module 'modules' from '<path>/modules/__init__.pyc'>
<module 'modules.update' from '<path>/modules/update.pyc'>
<class 'modules.update.update'>
update_name
I think now you can manipulate the code however you want to get any object you like, or even pass them as arguments like update
and name
earlier.
Edit:
you can not call unbounded method, you need to create an instance of that class and then call the method for that instance, that's what the error is saying. so you need to do something like in the following example:
import sys
arguments = sys.argv
module = arguments[1]
command = arguments[2]
moduleObj = __import__(module) # returns module "modules"
submodule = getattr(moduleObj, module) # returns the module "modules.update"
localInstance = submodule.update() # create instance of class 'update'
localInstance.test() # call instance method
and this will print:
test complete!
PS:
It is not encouraged to use some words for your method names or variables like system
or all
, better change them.
hope this helps.