Python3插件系统

Pie*_*e86 2 python plugin-architecture

我正在尝试创建一个类似于yapsy的插件框架(不幸的是yapsy不兼容python3).

我的代码看起来像这样:

root
   main.py
   plugins/
       __init__.py
       PluginManager.py
       UI/
           __init__.py
           textui.py
Run Code Online (Sandbox Code Playgroud)

在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
Run Code Online (Sandbox Code Playgroud)

其中candidate_filepath包含插件路径.

textui.py包含以下内容:

from root.plugins.PluginManager import UI_Plugins

class TextBackend(UI_Plugins):
    def run(self):
        print("c")
Run Code Online (Sandbox Code Playgroud)

当我尝试加载插件时,我收到此错误:

No module named plugins.PluginManager 
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题?

小智 5

对不起,这肯定不是你的问题的直接答案,但是如果你正在尝试为python3开发非常接近yapsy的东西,那么你可能会对我发布了几个python3的新版yapsy感兴趣兼容包:

https://sourceforge.net/projects/yapsy/files/Yapsy-1.9/

(见Yapsy-1.9_python3-py3.2.egg或Yapsy-1.9-python3.tar.gz)

源代码位于特定分支上:

http://yapsy.hg.sourceforge.net/hgweb/yapsy/yapsy/file/91ea058181ee