I have a virtual layer that I have created using the virtual layer creation tools provided by QGIS. Is there a way to load this layer in Python using QgsVectorLayer for instance? The vector layer doesn't have a clear path since it's stored in the memory. I have been able to bypass this issue by using :

MyLayer = iface.activeLayer()

However I want to be able to specify my layer without it being my active layer. I stumbled upon QgsVirtualLayer while doing some research but I'm not really sure about how to use it with only my layer's name. I feel like this is pretty basic but I'm still only getting started with Python and QGIS.

  • 1
    Memory layer ? my_point_memory_layer = QgsVectorLayer("Point", "my_layer_name", "memory"). Or search through project layers by name : searched_layers = QgsProject.instance().mapLayersByName("my_layer_name") (will return a list of all project layers with the same name, you can pick the first with my_layer = searched_layers[0]. Jan 6 at 12:45
  • Thank you, your second solution works just fine. For some reasons, your first suggestion doesn't seem to work. I have thought about it before but couldn't get it to work. I'll stick to the other one.
    – Gogo
    Jan 6 at 13:31
  • The first one is to create an empty memory layer in your project, not to retrieve one. Jan 6 at 13:40