I'm trying to study PyQGIS scripting and follow a tutorial. Following this tutorial I'm developing a plugin that returns "Hello World". I followed every step and copied all of the source code but get the following error message.

ImportError: cannot import name 'TestPlugin' from 'Plugin' (C:/Users/denni/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\Plugin__init__.py) Traceback (most recent call last): File "C:/OSGEO4~1/apps/qgis/./python\qgis\utils.py", line 335, in startPlugin plugins[packageName] = package.classFactory(iface) File "C:/Users/denni/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\Plugin__init__.py", line 2, in classFactory from Plugin import TestPlugin ImportError: cannot import name 'TestPlugin' from 'Plugin' (C:/Users/denni/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\Plugin__init__.py)

I will also post the code in the _init.py and Plugin.py files.

def classFactory(iface):
    from Plugin import TestPlugin
    return TestPlugin(iface)

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class TestPlugin:
    def __init__(self, iface):
        self.iface = iface

    def initGui(self):
        self.action = QAction("Run", self.iface.mainWindow())
        QObject.connect(self.action, SIGNAL("triggered()"), self.onRun)
        self.iface.addPluginToMenu("Test Plugin:", self.action)

    def unload (self):
        self.iface.removePluginMenu("Test Plugin:", self.action)

    def onRun(self):
        QMessageBox.information(self.iface.mainWindow(), "debug", "Hello World")

Where is the problem?

  • 1
    Are you sure that the tutorial is specific for QGIS 3 ? if it was made for QGIS 2 the code probably won't work due to the python version change between the two QGIS version – J.R May 24 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.