Remove plugin module path when unloading

Unloading plugin must remove added module path
    in order to prevent wrong package import when reloading
This commit is contained in:
David Marteau 2019-04-23 23:11:57 +02:00
parent 736327634e
commit 6af744da82

View File

@ -28,7 +28,7 @@ QGIS utilities module
""" """
from qgis.PyQt.QtCore import QCoreApplication, QLocale, QThread from qgis.PyQt.QtCore import QCoreApplication, QLocale, QThread, qDebug
from qgis.PyQt.QtWidgets import QPushButton, QApplication from qgis.PyQt.QtWidgets import QPushButton, QApplication
from qgis.core import Qgis, QgsMessageLog, qgsfunction, QgsMessageOutput from qgis.core import Qgis, QgsMessageLog, qgsfunction, QgsMessageOutput
from qgis.gui import QgsMessageBar from qgis.gui import QgsMessageBar
@ -452,12 +452,23 @@ def _unloadPluginModules(packageName):
if hasattr(sys.modules[mod], 'qCleanupResources'): if hasattr(sys.modules[mod], 'qCleanupResources'):
sys.modules[mod].qCleanupResources() sys.modules[mod].qCleanupResources()
except: except:
pass # Print stack trace for debug
qDebug("qCleanupResources error:\n%s" % traceback.format_exc())
# try removing path
if hasattr(sys.modules[mod], '__path__'):
for path in sys.modules[mod].__path__:
try:
sys.path.remove(path)
except ValueError:
# Discard if path is not there
pass
# try to remove the module from python # try to remove the module from python
try: try:
del sys.modules[mod] del sys.modules[mod]
except: except:
pass qDebug("Error when removing module:\n%s" % traceback.format_exc())
# remove the plugin entry # remove the plugin entry
del _plugin_modules[packageName] del _plugin_modules[packageName]