
Originally Posted by
y.a.k
Nice idea but I would use the application's private directory instead to prevent conflicts.
You can use any directory since import uses the sys.path to find the module. So, you can create a "plugins" subdirectory in your app's directory, add it to path and download the plugins there.
I'm trying use "plugins" subdirectory inside my app's directory but without complete success. I'm creating a signed SIS-package with ensymble's py2sis-command.
If I put my 2 test-plugins (test1_plugin.py and test2_plugin.py) to e.g. "E:\Python\plugins" and then sys.path.append("E:\\Python\\plugins")
those plugins can be __import__:ed successfully.
If I put them to the apps main directory those plugins are __import__:ed successfully without need to append anything to the sys.path (because "C:\Private\0xUID\" is already in the sys.path).
But if I create plugins-subdirectory and then try __import__ plugins from there, python claims: ImportError: No module named test1_plugin. (I use "exception harness" to catch traceback in SIS-packaged app.)
What I'm doing wrong?
Here is how I add plugins-dir to the sys.path in the last case:
Code:
self.plugin_dir = os.path.join(os.getcwd(), u"plugins")
appuifw.note(u"%s" % self.plugin_dir, "info")
sys.path.append(self.plugin_dir)
# sys.path contains now C:\Private\0xUID\ AND C:\Private\0xUID\plugins
And here is the function to get a list of available plugins, works as expected:
Code:
def get_plugins(self):
"""
Get a list of available plugins.
All files starting with a letter and ending with .py
are considered as a plugin.
"""
r = re.compile(r'^([a-zA-Z]+[\w]*)\.py$')
pluginlist = []
for file in os.listdir(self.plugin_dir):
matches = r.match(file)
if matches is not None and matches.groups()[0] != 'BasePlugin':
#print matches.groups()[0]
pluginlist.append(matches.groups()[0])
else:
pass
#print "Not included: " + file
return pluginlist
And here is the snippet which hopefully shows how I try to import plugins:
Code:
# "plugin" contains plugin's name e.g. test1_plugin
for plugin in self.get_plugins():
appuifw.note(u"%s" % plugin, "info")
# THIS RAISES ImportError:
self.mods[plugin] = __import__(plugin)
plugin_instance = self.mods[plugin].Plugin(self)
self.insts.append(plugin_instance)
self.menu_entries.append(((plugin_instance.title), plugin_instance))