How can I get a class object/instance from a previous __import__ in Python?
Question: Let's suppose you have a module named "module" inside a hirearchical "folder2"
with a class named "klass".

How can I get the class like in a square bracket syntax?

    
    
    module = __import__('folder1.folder2.module', fromlist='*')
    sbs_module = module['klass']
    
    #lets say we print a value from the method getValue
    print sbs_module.getValue()
    


Answer: Replace this line:

    
    
    sbs_module = module['klass']
    

with this line:

    
    
    sbs_module = module.klass
    

The reason for that is that you need to refer to module variables (in this
case variable named `klass`) by using dot notation, not by accessing the
variables by keys.

Alternatively you can use two different notations:

  * by key:
    
        sbs_module = vars(module)['klass']
    

  * by attribute:
    
        sbs_module = getattr(module, 'klass')
    

but I do not recommend it unless absolutely necessary.

