Skip to content

Commit 5c2f70b

Browse files
committed
Remove plugin module path when unloading
Unloading plugin must remove added module path in order to prevent wrong package import when reloading
1 parent 2d67f3f commit 5c2f70b

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

python/utils.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
2929
"""
3030

31-
from qgis.PyQt.QtCore import QCoreApplication, QLocale, QThread
31+
from qgis.PyQt.QtCore import QCoreApplication, QLocale, QThread, qDebug
3232
from qgis.PyQt.QtWidgets import QPushButton, QApplication
3333
from qgis.core import Qgis, QgsMessageLog, qgsfunction, QgsMessageOutput
3434
from qgis.gui import QgsMessageBar
@@ -411,12 +411,23 @@ def _unloadPluginModules(packageName):
411411
if hasattr(sys.modules[mod], 'qCleanupResources'):
412412
sys.modules[mod].qCleanupResources()
413413
except:
414-
pass
414+
# Print stack trace for debug
415+
qDebug("qCleanupResources error:\n%s" % traceback.format_exc())
416+
417+
# try removing path
418+
if hasattr(sys.modules[mod], '__path__'):
419+
for path in sys.modules[mod].__path__:
420+
try:
421+
sys.path.remove(path)
422+
except ValueError:
423+
# Discard if path is not there
424+
pass
425+
415426
# try to remove the module from python
416427
try:
417428
del sys.modules[mod]
418429
except:
419-
pass
430+
qDebug("Error when removing module:\n%s" % traceback.format_exc())
420431
# remove the plugin entry
421432
del _plugin_modules[packageName]
422433

0 commit comments

Comments
 (0)