python - How to access a class from a dynamically imported module -


i have dynamically imported module:

module_object = importlib.import_module(audio_module) 

now if list of attributes associated module:

print dir(module_object) 

i following:

['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'aud_shell', 'shlex', 'sys'] 

i want access "aud_shell" class located within module. since module dynamically imported, not have knowledge of class has within it. how search within imported module class name , access class?

for example:

  1. dynamically import module
  2. obtain name of class within module
  3. access method within class

in end have this:

module_object.aud_shell.some_method() 

i hope explained correctly. let me know if didn't make sense. thanks.

actually 5 minutes after posting question able solve it, in case else trying same thing here is:

after have imported module dynamically:

module_object = importlib.import_module(audio_module) 

you can obtain class doing following:

module_class = inspect.getmembers(module_object, inspect.isclass) 

then need create object of class module. done by:

class_object = getattr(module_object, module_class) 

now when print "class_object" following:

>>>print class_object audio_module.aud_shell 

now need figure out how list of methods within class , access methods...


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -