How to find full module path of a class to import in other file
Question: I have method that returns module path of given class name

    
    
    def findModulePath(path, className):
        attributes = []
        for root, dirs, files in os.walk(path):
            for source in (s for s in files if s.endswith(".py")):
                name = os.path.splitext(os.path.basename(source))[0]
                full_name = os.path.splitext(source)[0].replace(os.path.sep, '.')
                m = imp.load_module(full_name, *imp.find_module(name, [root]))
                try:
                    attr = getattr(m, className)
                    attributes.append(attr)
                #                if "." in attr.__module__:
                #                    return
    
                except:
                    pass
        if len(attributes) <= 0:
            raise Exception, "Class %s not found" % className
    
        for element in attributes:
            print "%s.%s" % (element.__module__, className)
    

but it does not return the full path of the module, For example I have a
python file named "objectmodel" in objects package,and it contains a Model
class, So I call findModulePath(MyProjectPath,"Model"). it prints
objectmodel.Model but I need objects.objectmodel.Model


Answer: The attribute you're looking for is `__file__`. Note you may have to do some
massaging of this value after you get it - it could be a `.py`, `.pyc`,
`.pyd`, `.so`, `.dll`, etc.

Of course it's also going to be a full path, but you have your root which you
can subtract to get the actual hierarchy that you care about.

