Skip to content

Commit

Permalink
Flush python module cache before importing included version of zeroconf
Browse files Browse the repository at this point in the history
  • Loading branch information
fieldOfView committed Jun 2, 2022
1 parent f0d9e8f commit f859e09
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions OctoPrintOutputDevicePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
# expand search path so local copies of zeroconf and ifaddr can be imported
import sys

original_path = list(sys.path)

if "zeroconf" in sys.modules:
Logger.log("d", "The zeroconf module is already imported; trying to flush it so we can import our own version")
sys.modules.pop("zeroconf")

plugin_path = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(plugin_path, "ifaddr"))
sys.path.insert(0, os.path.join(plugin_path, "python-zeroconf"))
Expand All @@ -53,13 +59,16 @@
)

# restore original path
del sys.path[0] # python-zeroconf
del sys.path[0] # ifaddr
sys.path = original_path

Logger.log("d", "Using included Zeroconf module version %s" % zeroconf_version)
except (FileNotFoundError, ImportError) as exception:
# fall back to the system-installed version, or what comes with Cura
Logger.logException("e", "Failed to load included version of Zeroconf module")

# restore original path
sys.path = original_path

from zeroconf import (
Zeroconf,
ServiceBrowser,
Expand Down

0 comments on commit f859e09

Please sign in to comment.