Q:

PyQGIS add feature in layer with Value Relations in columns

Have a point layer (from geopackage) with value ralations widget in several columns. Trying to add new feature to this layer with the following code:
import uuid
layers = QgsProject.instance().mapLayersByName('L10_Public--Point')
layer = QgsVectorLayer(layers[0].dataProvider().dataSourceUri(), '', 'ogr')
print(layer)

caps = layer.dataProvider().capabilities()

if caps & QgsVectorDataProvider.AddFeatures:
    feat = QgsFeature(layer.fields())
    feat.setAttributes([2, 'Public', NULL, str(uuid.uuid4()), str(uuid.uuid4()), '602010904', NULL, NULL, '97629445', NULL, 5.0, 2.0, 3.0, 4.0, NULL, NULL, 4.0, NULL, NULL, NULL, NULL, 4.0, 4.0])
    feat.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(1605057.281,645118.237)))
    res, outFeats = layer.dataProvider().addFeatures([feat])

And getting:
Traceback (most recent call last):
  File "C:\PROGRA~1\QGIS3~1.10\apps\Python37\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "<string>", line 2, in <module>
IndexError: list index out of range

Should be mentionel that the same code works well for another layer, which has no widgets in columns. Anybody knows what IndexError means here? Or how can I programmatically add new features in such a layer?

A:

IndexError: list index out of range means that you are trying to access an element of the list which does not exist. That's, layers list is empty, there is no a layer named L10_Public--Point in your project or somehow layers = QgsProject.instance().mapLayersByName... should be returning empty list.

