In the QGIS (2.99) Python Console, the name/variable qgis seems to be automatically defined.

However, in writing a plugin, there is an error whenever I refer to qgis. For example, in I have qgis.iface.mapCanvas() in my Plugin's `initGui(self)' function, I get an error

NameError: name `qgis` is not defined

How can fix this error?

By the way, I am trying to put the following code from this GIS.SE answer(https://gis.stackexchange.com/a/45105) into my plugin

tool = PointTool(qgis.iface.mapCanvas())

The code works fine inside QGIS main window (in Python Console), but generates the above error when moving it into the Plugin. I can't figure out how to let the PointTool respond inside a plugin. The code for PointTool class from the linked answer is:

class PointTool(QgsMapTool):   
    def __init__(self, canvas):
        QgsMapTool.__init__(self, canvas)
        self.canvas = canvas    

    def canvasPressEvent(self, event):

  • Does qgis.utils.iface.mapCanvas() not work in your plugin or is qgis.utils now obsolete?
    – Joseph
    Commented Jun 22, 2017 at 9:22

1 Answer 1


When you create a plugin, you receiv iface via the __init__ function :

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

After that in you code you can access to iface via self.iface.

Otherwise you can have access via qgis.utils.iface

  • This should be accepted as an answer - it works for me by simply adding this def and then changing any references.
    – GeorgeC
    Commented Mar 3, 2019 at 5:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.