I have created a QGIS plugin and I am using the following code to get my layers by name from the layer panel on QGIS.

Here is the simplified code:

layer1 = QgsMapLayerRegistry.instance().mapLayersByName("layer1")[0]
layer2 = QgsMapLayerRegistry.instance().mapLayersByName("layer2")[0]
layer3 = QgsMapLayerRegistry.instance().mapLayersByName("layer3")[0]

But if any of these three layers are missing, then my plugin crashes with the error:

out of index

because it cannot find the missing layer.

How can I create a condition to stop the code from crashing every time there is a missing layer, and instead just show me a simple message e.g.: layer1 missing from the layer panel?

  • 3
    try: except: docs.python.org/3/tutorial/errors.html would be the simplest way. In your except block you could use sys.exit(Value), where Value is any number but usually negative to indicate an error code, to ungracefully but immediately bail out of execution otherwise the script should just keep running from the next line. Jun 6, 2017 at 21:29
  • 2
    @MichaelMiles-Stimson - Think you should post that as an answer and probably use iface.messageBar().pushWarning( u'ERROR: ', 'layer(x) missing from layer panel' ) in the except block to show up a warning message if a layer doesn't match :)
    – Joseph
    Jun 7, 2017 at 10:13
  • @Joseph yes that good but first maybe I need some condition to say something like this if this layer name is not in qgis then iface.messageBar().pushWarning( u'ERROR: ', 'layer(x) missing from layer panel' ) ?
    – Mar
    Jun 7, 2017 at 13:58
  • 1
    @Joseph, try: except: is general python which is off-topic for this site but iface.messageBar... is QGIS specific which makes it on-topic for this site. I think you have more of an answer in your comment than I. If you have been able to solve this problem Mar based on the comments please consider answering your own question, with a working code block, for the benefit of future users. Jun 7, 2017 at 21:06

1 Answer 1


To bypass the error, I suggest you to do to best practices.

  1. Before you try to access the value using an index from a list, check the length of the list. If the list is empty, you will see a stack trace named IndexError. Doing the following will prevent it.

     layers1 = QgsMapLayerRegistry.instance().mapLayersByName("layer1")
     layers2 = QgsMapLayerRegistry.instance().mapLayersByName("layer2")
     layers3 = QgsMapLayerRegistry.instance().mapLayersByName("layer3")
     if len(layers1) > 0:
         #do stuff
         layer1 = layers1[0]
     if len(layers2) > 0:
         layer2 = layers2[0]
         #do stuff
     if len(layers3) > 0:
         layer3 = layers3[0]
         #do stuff
  2. You can also escape the IndexError but I do not see the need if you can use the above code.

         layer1 = QgsMapLayerRegistry.instance().mapLayersByName("layer1")[0]
         layer2= QgsMapLayerRegistry.instance().mapLayersByName("layer2")[0]
         layer3 = QgsMapLayerRegistry.instance().mapLayersByName("layer3")[0]
     except IndexError as ex:
         print ex
         # send warning...

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.