yapsyに似たプラグインフレームワークを作成しようとしています(残念ながら、yapsyはpython3と互換性がありません)。Python3プラグインシステム
私のコードは次のようになります。
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は、プラグインのパスが含まれていますPluginManager.pyで
root
main.py
plugins/
__init__.py
PluginManager.py
UI/
__init__.py
textui.py
私は以下のクラスを定義しました。
textui.pyには、次のものが含まれています
from root.plugins.PluginManager import UI_Plugins
class TextBackend(UI_Plugins):
def run(self):
print("c")
私はこのエラーを取得するプラグインをロードしようとすると:
No module named plugins.PluginManager
どのように私はこの問題を解決することができますか?
大変申し訳ありません。私が間違えました!申し訳ありませんが、元の投稿を修正しました。 – Pie86
OK、返信も更新しました。 –