I am loading all layers from a geopackage in a QGIS 3 canvas in Python using a list with all layernames and the following code:

databasepath = "X:/test.gpkg"
layer = ["1","2","3","4"]    
for i in layer:    
   iface.addVectorLayer(databasepath+"|layername="+i, i, 'ogr')

Is there a better way to load all layers from a geopackage beside the list as the layer names or count may change?

1 Answer 1


The following code uses OGR to read all layers' names in a Geopackage and adds all layers to the current QGIS project:

from osgeo import ogr

databasepath = r"X:/test.gpkg"
conn = ogr.Open(databasepath)

for i in conn:
    iface.addVectorLayer(databasepath + "|layername=" + i.GetName(), i.GetName(), 'ogr')
  • Nice code but is there any way to make sure Database Path can be set in the form of a pop up? Jul 23, 2020 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.