1

In my python application I import some configuration related the module that I want to import, so how to do that using string conf like shown in below example?

For exmpple I use pakage

package="pak.pak1"
module="moduleA"

And I want to do folwing

from package import module 

but i have the error:

ModuleNotFoundError: No module named moudleA

Ayoub Benayache
  • 1,046
  • 12
  • 28
  • does your package have `__init__.py`? – Jahongir Rahmonov Sep 06 '17 at 16:13
  • Maybe this helps you: https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path – philippd Sep 06 '17 at 16:13
  • yes i have it ,, my probelm is how to use this importation as a confugration saved in string variable – Ayoub Benayache Sep 06 '17 at 16:43
  • I believe you may have a typo somewhere. You have `module="moduleA"`, but your error is saying `moudleA`. They aren't spelled the same. – jape Sep 06 '17 at 21:39
  • this is just an exmple not a real code, any where i find a solution to my question its laike this – Ayoub Benayache Sep 07 '17 at 22:25
  • my code def m(): import importlib function_string ='m.aa.bb.show' pak,mod_name,clas_name, func_name = function_string.rsplit('.') mod = importlib.import_module(pak+'.'+mod_name) print(mod_name) print(clas_name) print(func_name) clas=getattr(mod, clas_name) func = getattr(clas, func_name) func(33) m() ////////////////// – Ayoub Benayache Sep 07 '17 at 22:26
  • class aa: def serv(self): print("hello wrold") class bb: def show(ff): print('gggggggggggggg',ff) def h(g): print("ppppppppppppppppp ",g) – Ayoub Benayache Sep 07 '17 at 22:26

1 Answers1

0
class aa:
    def serv(self):
        print("hello wrold")
class bb:
    def show(ff):
        print('gggggggggggggg',ff)

def h(g):
    print("ppppppppppppppppp  ",g)
#/////////////////////////////////////////
def m():
    import importlib
    function_string ='m.aa.bb.show'
    pak,mod_name,clas_name, func_name = function_string.rsplit('.')
    mod = importlib.import_module(pak+'.'+mod_name)
    print(mod_name)
    print(clas_name)
    print(func_name)
    clas=getattr(mod, clas_name)
    func = getattr(clas, func_name)
    func(33)   
m()
Ayoub Benayache
  • 1,046
  • 12
  • 28