I need to select a specific layer in the layer tree before I run my python script. I have tried Activating layer by its name in PyQGIS? with the code

registry = QgsProject.instance()
layer = registry.mapLayersByName("name")[0]

but on the layer tree, there is no change of focus. The layer does not get selected. How can I accomplish that?

  • 2
    iface.setActiveLayer(layer). A similar question was asked here. – Joseph Jul 23 at 13:37
  • 1
    This is perfect! – GISme Jul 23 at 13:45

Browse other questions tagged or ask your own question.