我正在尝试创建一个类似于 yapsy 的插件框架(不幸的是 yapsy 不兼容 python3)。
我的代码如下所示:
root
main.py
plugins/
__init__.py
PluginManager.py
UI/
__init__.py
textui.py
在 PluginManager.py 我定义了以下类:
class PluginMetaclass(type):
def __init__(cls, name, base, attrs):
if not hasattr(cls, 'registered'):
cls.registered = []
else:
cls.registered.append((name,cls))
class UI_Plugins(object):
__metaclass__ = PluginMetaclass
#...some code here....
def load():
#...some code here too...
if "__init__" in os.path.basename(candidate_filepath):
sys.path.append(plugin_info['path'])
try:
candidateMainFile = open(candidate_filepath+".py","r")
exec(candidateMainFile,candidate_globals)
except Exception as e:
logging.error("Unable to execute the code in plugin: %s" % candidate_filepath)
logging.error("\t The following problem occured: %s %s " % (os.linesep, e))
if "__init__" in os.path.basename(candidate_filepath):
sys.path.remove(plugin_info['path'])
continue
其中 Candidate_filepath 包含插件路径。
textui.py 包含以下内容:
from root.plugins.PluginManager import UI_Plugins
class TextBackend(UI_Plugins):
def run(self):
print("c")
当我尝试加载插件时,出现此错误:
No module named plugins.PluginManager
我怎么解决这个问题?