There are several ways to access the layers.
As @christoph tells you, it is easier and more direct to use the methods of QgsProject
.
We start by instantiating pry=QgsProject.instance()
1.- To access a layer by its name you can use
pry.mapLayersByName()
It returns a list, because you can have several layers with the same name. If only one layer matches, you can access it by its index:
pry.mapLayersByName("transport")[0]
2.- From QGIS3.10 you also have the mapLayersByShortName()
method
3.- If you want a list with all the layers:
pry.mapLayers()
Returns a dictionary, the layer id is the key
On the other hand, you can combine with the methods of iface (QgisInterface) and QgsMapCanvas
1.- Get the active layer**:
iface.activeLayer()
returns the active layer
iface.mapCanvas().currentLayer()
generates the same result
2.- A list of the layers visible on the canvas
iface.mapCanvas().layers()