From 5797af88610db17fb6ab96a2f581bd4e8cbdf84f Mon Sep 17 00:00:00 2001 From: Jonathan Harris Date: Sat, 24 Jun 2017 00:41:40 +0100 Subject: [PATCH] Add plugins' folders to sys.path --- PLUGINS.md | 14 +------------- plug.py | 10 +++++++--- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/PLUGINS.md b/PLUGINS.md index 49ae82df..1a67efcd 100644 --- a/PLUGINS.md +++ b/PLUGINS.md @@ -143,19 +143,7 @@ To package your plugin for distribution simply create a `.zip` archive of your p * Windows: In Explorer right click on your plugin's folder and choose Send to → Compressed (zipped) folder. * Mac: In Finder right click on your plugin's folder and choose Compress. -If there are any external dependencies then bundle them within the plugin's folder and add the plugin's folder to Python's load path. - -```python -import sys -import os - -# Add plugin's folder to Python's load path. -sys.path.append(os.path.join(os.path.dirname(__file__))) - -# Assumes the dependency has been bundled in the plugin's folder. -import paho.mqtt.client as mqtt - -``` +If there are any external dependencies then include them in the plugin's folder. # Disable a plugin diff --git a/plug.py b/plug.py index 2fa7d5aa..6e497e2e 100644 --- a/plug.py +++ b/plug.py @@ -45,11 +45,15 @@ def load_plugins(): for plugname in disabled: sys.stdout.write("plugin {} disabled\n".format(plugname)) - for plugname in found: + for plugname, loadfile in found.iteritems(): try: sys.stdout.write("loading plugin {}\n".format(plugname)) - with open(found[plugname], "rb") as plugfile: - plugmod = imp.load_module(plugname, plugfile, found[plugname].encode(sys.getfilesystemencoding()), + + # Add plugin's folder to Python's load path in case plugin has dependencies. + sys.path.append(os.path.dirname(loadfile)) + + with open(loadfile, "rb") as plugfile: + plugmod = imp.load_module(plugname, plugfile, loadfile.encode(sys.getfilesystemencoding()), (".py", "r", imp.PY_SOURCE)) if hasattr(plugmod, "plugin_start"): newname = plugmod.plugin_start()