Is it possible to find out the path to a subclass file in python?

I have a plugin system. A subclass of a subclass from a common ancestor ... the declaration is as follows:

-- SDK --- basePlugin.py -- PLUGINS --- PluginA ---- Plugin.py ---- Config.ini --- PluginB ---- Plugin.py ---- Config.ini 

I need to read Config.ini information in the basePlugin.py __init__ file. CUrrently in every plugin I do:

 class PluginA(BaseSync): __init__(self, path): super(PluginA,self).__init__(self, __file__) 

But I wonder if it is possible to find out in the parent class which file the subclass is in ...

+6
python inheritance plugins
source share
1 answer

Assuming BaseSync is a new-style class, the parent BaseSync class can find the file that defines PluginA as follows:

 import sys class BaseSync(object): def __init__(self): path=sys.modules[self.__module__].__file__ 

(so you do not need to explicitly pass path ).

+9
source share

All Articles