我想创建一个插件系统,我有一个将所有模块导入到数组中的函数。插件的多个导入相同的文件名
布局:
PLUGINSDIR/
PLUGINSDIR /聊天
PLUGINSDIR /聊天/ main.py
这是查找并导入功能插件:
if os.path.exists(pluginsDir):
for path, dirArray, fileArray in os.walk(pluginsDir):
for fileName in fileArray:
if fileName == "main.py":
sys.path.append(path)
try:
plugins.append(__import__("main"))
except:
print 'Could not import plugin, "'+path+'": plugin contains errors or is not a real plugin.'
这很好,如果我只有一个插件,但是当我有多个插件时,它会不断导入它检测到的第一个插件。插件的
布局:
PLUGINSDIR/
PLUGINSDIR /聊天
PLUGINSDIR /聊天/ main.py
PLUGINSDIR /建
PLUGINSDIR /编译/主.py
我试过在我的try语句后添加sys.path.remove(path)
,但是它并没有在我已经导入模块后删除路径。
我能做些什么来正确导入我的插件?
那是因为我修改我的代码更可读的就在这里,但我最终带走了一些缩进:
有了Python包,在你的脚本中的循环,可以方便地与实施。 – MetaDark 2010-12-22 16:51:35