How to import a module given its name as string?

前端 未结 11 1395
不思量自难忘°
不思量自难忘° 2020-11-21 06:19

I\'m writing a Python application that takes as a command as an argument, for example:

$ python myapp.py command1

I want the application to

11条回答
  •  攒了一身酷
    2020-11-21 06:53

    The below piece worked for me:

    >>>import imp; 
    >>>fp, pathname, description = imp.find_module("/home/test_module"); 
    >>>test_module = imp.load_module("test_module", fp, pathname, description);
    >>>print test_module.print_hello();
    

    if you want to import in shell-script:

    python -c ''
    

提交回复
热议问题