I want to get the Geopackage file path where my layer is stored in.

Layer = QgsProject.instance().mapLayersByName('DAT')[0]

But this only gives me the path with layer pointer:


My approach was to do:

Layer = Layer.source().split("|")[0]

It works, but is there a better way?

  • 1
    Does this answer your question? Getting path of project, or layer file in PyQGIS?, where you may be interested in this approach: >>>layerProvider = layer.dataProvider() >>>FilePath = layerProvider.dataSourceUri() >>>FilePath = os.path.dirname(unicode(FilePath))
    – Taras
    Feb 7, 2022 at 12:24
  • 1
    I believe that's about right. Pretty similar than to get the data provider and its uri dp = my_lyr.dataProvider() print(dp.uri()).
    – user30184
    Feb 7, 2022 at 12:25


Browse other questions tagged or ask your own question.