I need a list containing the names of all the layers in a QGIS session. I did the task as

layersNames = []
for i in self.iface.mapCanvas().layers():

but this has the problem that only the names for the visible layers are extracted. How can I get a list with the names of all (visible or not) layers using PyQGIS?


QgsMapLayerRegistry.instance().mapLayers() will give you all layers that are opened.

If you want the names then:

names = [layer.name() for layer in QgsMapLayerRegistry.instance().mapLayers().values()]

names will be a list of layer names

or using a normal function:

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
    print layer.name()

Since version 3, QgsMapLayerRegistry funcionalities have been moved to QgsProject: https://qgis.org/api/api_break.html

Update for QGIS3.x:

from qgis.core import QgsProject
names = [layer.name() for layer in QgsProject.instance().mapLayers().values()]

as per @Nathan W's answer, this produces a list of layers in the current project:

['GoogleSat', 'MyPointsLayer', 'Roads', 'House_numbers']

  • How does it work in the new version to get back just active layers? – James Norris Sep 8 '20 at 14:23

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.