Skip to content

Commit 6af744d

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 7363276 commit 6af744d

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
@@ -452,12 +452,23 @@ def _unloadPluginModules(packageName):
452452
if hasattr(sys.modules[mod], 'qCleanupResources'):
453453
sys.modules[mod].qCleanupResources()
454454
except:
455-
pass
455+
# Print stack trace for debug
456+
qDebug("qCleanupResources error:\n%s" % traceback.format_exc())
457+
458+
# try removing path
459+
if hasattr(sys.modules[mod], '__path__'):
460+
for path in sys.modules[mod].__path__:
461+
try:
462+
sys.path.remove(path)
463+
except ValueError:
464+
# Discard if path is not there
465+
pass
466+
456467
# try to remove the module from python
457468
try:
458469
del sys.modules[mod]
459470
except:
460-
pass
471+
qDebug("Error when removing module:\n%s" % traceback.format_exc())
461472
# remove the plugin entry
462473
del _plugin_modules[packageName]
463474

0 commit comments

Comments
 (0)