I am Esri-trained, trying to move into QGIS using Python. Simply trying to add a vector layer in QGIS returns the below error:

TypeError: QgisInterface.addVectorLayer(QString, QString, QString): first argument of unbound method must have type 'QgisInterface'

Here's my code:

layer = qgis.core.QgsVectorLayer("C:\mypath\mypath\GTM_adm1.shp", "GTM_adm1", "ogr")
qgis.gui.QgisInterface.addVectorLayer(layer, "GTM_adm1", "ogr")

Not sure what I'm missing here but the Vector layer object is in the registry.


Try simply:

layer = iface.addVectorLayer("/path/to/shapefile/file.shp", "layer_name_you_like", "ogr")

Here you have a nice guide for PyQGIS: http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/intro.html

  • thanks, this worked fine. But might you offer a quick explanation as to why my code returned an error? The logic is not obvious to me. – mikeLdub Jul 6 '15 at 16:09
  • Your method wants as first argument an instance of class. Here I found a nice explanation: julien.danjou.info/blog/2013/… – dmh126 Jul 6 '15 at 16:31
  • thanks, for the link. It's a very useful explanation and has helped clarify some concepts for me. – mikeLdub Jul 6 '15 at 18:27

Just as a remark: QgisInterface is an abstract class and has virtual functions so it may not be instantiated, i.e. it defines an interface and not a complete implementation. But you can use QgisInterface class within a standalone application where the iface class is not available. There you can reimplement and override the main functions of the class. An example very trivial is the following:

from qgis.core import QgsMapLayerRegistry, QgsVectorLayer
from qgis.gui import QgisInterface

def addVectorLayer(uri, provider, name):
    vl = QgsVectorLayer(uri, name, provider)
    return vl, name

class MyIface(QgisInterface):
    def __init__(self):

    def addVectorLayer(self, path, name, provider):
        return addVectorLayer(path, provider, name)

# usage:
# >>> myiface = MyIface()
# >>> myiface.addVectorLayer("some_path_to_layer", "some_name", "ogr")

So using

>>> myiface.addVectorLayer("some_path_to_layer", "some_name", "ogr")
(<qgis._core.QgsVectorLayer object at 0x7546440>, 'some_name')

it will work, but another method e.g

>>> myiface.activeLayer()
NotImplementedError: QgisInterface.activeLayer() is abstract and must be overridden

will raise to NotImplementedError because that method is defined into the class but it is not re-implemented, i.e. it is an abstract method.

BTW: your error (...first argument of unbound method...) is a different error though, the origin of the error is you don't have instantiated the class before, so the correct way to write was:

qgis.gui.QgisInterface().addVectorLayer(layer, "GTM_adm1", "ogr")

which would raise to TypeError: qgis._gui.QgisInterface represents a C++ abstract class and cannot be instantiated

Hope this helps as explanation.

Your Answer

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

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