I am trying to migrate a plugin from QGIS 2 to QGIS 3. This function doesn't work with QGIS 3 :

def getLayersByKeyWord(keyword):
    #retourne une liste de layer
    layerMap = QgsMapLayerRegistry.instance().mapLayers()
    for name, layer in layerMap.iteritems():
        if layer.keywordList() == keyword:
    if len(res)>0:
        return res
        return None

I tried to replace QgsMapLayerRegistry with QgsProject but it is still not working on iteritems().

I still have this error : for name, layer in layerMap.items: TypeError: 'builtin_function_or_method' object is not iterable

  • Don't forget parenthesis. It should be .items() not .items – Kadir Şahbaz Sep 29 '18 at 17:44

You should change two lines. The first one is about PyQGIS 3, the other is about Python 3.

  1. Change QgsMapLayerRegistry.instance().mapLayers() into QgsProject.instance().mapLayers(). Please check this answer out for more information.

  2. Change layerMap.iteritems() into layerMap.items(). Please check What is the difference between dict.items() and dict.iteritems()? and answers for additional information.

| improve this answer | |

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.