I'm trying to create a new GeoPackage with an empty layer in it. I tried this code from this answer.

from qgis.core import *
from qgis.PyQt.QtCore import QVariant
QgsApplication([], False)

layer_name = 'my_layer'
gpkg_path = 'test.gpkg'
geom = QgsWkbTypes.MultiPolygon
crs = QgsCoordinateReferenceSystem('epsg:4326')
tc = QgsCoordinateTransformContext()
schema = QgsFields()
schema.append(QgsField('double_field', QVariant.Double))
schema.append(QgsField('text_field', QVariant.String))
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = "GPKG"
options.layerName = layer_name
writer = QgsVectorFileWriter.create(gpkg_path, schema, geom, crs, tc, options)
assert writer.hasError() == QgsVectorFileWriter.NoError
del writer  # to flush

But I can't read it

lyr = QgsVectorLayer('test.gpkg', 'my_layer', 'ogr')
assert lyr.isValid()  # False instead of true

In QGIS, the test GeoPackage have no layers inside instead of 'my_layer' as expected.

What I'm doing wrong?

  • Line schema.append(QgsField('double_field', QVariant.Double)) is freezing my QGIS and I can only kill it from operating system. What I'm doing wrong?
    – user30184
    Jan 28, 2022 at 13:59
  • I was using pyqgis standalone. If you are inside QGIS remove the 4 first lines. Jan 28, 2022 at 16:06
  • No problem with ogrinfo Open of 'test.gpkg' using driver 'GPKG' successful. 1: my_layer (Multi Polygon). Also attributes double_field and text_field are there. Maybe QGIS wants to get extent or feature count or something that is does not get from an empty layer. But GeoPackage seems OK to me.
    – user30184
    Jan 28, 2022 at 16:21
  • Not with my setup. javier@urano:~/osm-es/CatAtom2Osm/results$ ogrinfo test2.gpkg INFO: Open of test2.gpkg' using driver GPKG' successful. 1: prueba (Multi Polygon) javier@urano:~/osm-es/CatAtom2Osm/results$ ogrinfo test.gpkg INFO: Open of test.gpkg' using driver GPKG' successful. test2.gpkg was created inside QGIS with the menus. test.gpkg was created with the code. Jan 28, 2022 at 16:30
  • Did you run your code inside QGIS like I did by following your advice? The result for me is a completely valid GeoPackage according to the GDAL script validate_gpkg.py. And I do have a row in gpkg_contents.
    – user30184
    Jan 28, 2022 at 16:44

1 Answer 1


Had a similar case right now.

My solution was to remove options.layerName attribute (it can be defined in QgsVectorLayer), set QgsVectorFileWriter.create() as a separate variable and then delete it.

The full code:

out_layer = r'D:\GIS\Терпланирование\sheet_folder\demo5.gpkg'
schema = QgsFields()
schema.append(QgsField('id', QVariant.Int))

crs = QgsCoordinateReferenceSystem('epsg:4326')
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = "GPKG"
options.fileEncoding = 'cp1251'

fw = QgsVectorFileWriter.create(
del fw 
lyr = QgsVectorLayer(r"D:\GIS\Терпланирование\sheet_folder\demo5.gpkg", 'my_layer', 'ogr')    

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.