I'm new with PyQGIS. I need to load a raster layer from gpkg file, and i tried this way:

gpkg_raster_land_layer = "GPKG:" + path_to_gpkg + ":" + layername

r_layer = QgsRasterLayer(gpkg_raster_land_layer, layername, "gdal")
if not r_layer.isValid():
    print("Land layer failed to load!")

But it didn't work: Land layer failed to load! I didn't understand how to proceed to load the raster, can someone help me?

  • Yes, it print "Land layer failed to load!"
    – Sommo
    Mar 3, 2020 at 14:20
  • The print statement doesn't seem to match the action, though of course you may not be able to add a non valid raster. It would be useful probably to know why it's invalid, or what happens if just try to load add it without first doing a validity check. That might help you go further
    – nmtoken
    Mar 3, 2020 at 16:17

1 Answer 1


In my case, the following code works to load in QGIS a raster layer contained in a geopackage:

s_gpkg = "path_to_gpkg"
lyr_in = QgsRasterLayer(s_gpkg, "Test", "gdal")
if lyr_in.isValid():

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.