Getting list of layer names using PyQGIS?

Getting list of layer names using PyQGIS?

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(): layersNames.append(str(

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 = [ for layer in QgsMapLayerRegistry.instance().mapLayers().values()]

nameswill be a list of layer names

or using a normal function:

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

Since version 3,QgsMapLayerRegistryfuncionalities have been moved toQgsProject:

Update for QGIS3.x:

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

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

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