python - call a specific module using the module name as an argument to the main program -


what achieve this. have series of small modules in there own module directory. each module supplies same function. import modules in main program. have main program called module name , have return value module.

import worker_modules parser = argparse.argumentparser(description='download file through curl') parser.add_argument(     'module', help='module work on') args = parser.parse.args() module = args.module result = module.command(extra args not shown) 

the result fails thinks module has no attr command.

is there way tho achieve - dont want dynamically load module want have built static file.

you can @ imported modules using sys.modules

import sys  # parser code...  module = args.module result = sys.modules[module].command() 

this allows import modules needed @ beginning without dynamically importing them. when module imported added sys.modules dict.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -