I've been using ArcPy for quite a while, but I'm now trying to learn how to create geospatial workflows using PyQGIS. I'm starting with simple things, like adding a layer to the map. I'm trying with the following code:

import os

gdb_URL = r'...'
fc_path = os.path.join(gdb_URL, 'feature_class')
layer = iface.addVectorLayer(fc_path, 'layer_name','ogr')

That throws a message saying that 'the layer is not valid'.

=> If I use the same piece of code to add a shapefile instead of a feature class within a file geodatabase, the layer gets added without problems.

=> If I try to add the feature class within a file geodatabase using the "Open Data Source Manager" button in QGIS, the feature class gets added without problems.

Any ideas why that piece of code is not valid to add feature classes?

Maybe 'ogr' is wrong and I should be using a different provider name.

  • Are you using QGIS v2 or v3?
    – artwork21
    Commented Feb 8, 2019 at 12:37
  • QGIS version => 3.4.4-Madeira Commented Feb 8, 2019 at 12:43

1 Answer 1


Try this:

uri = "C:/Temp/my.gdb|layername=fcName"
layer = QgsVectorLayer(uri, "layer_name_you_like", "ogr")
  • I said that it didn't work but actually I hadn't realised that your suggestion had "|" before the layer name instead of "/". I have changed it and it works fine. Commented Feb 8, 2019 at 14:37
  • 1
    When I try with "layer = iface.addVectorLayer(fc_path, 'layer_name','ogr')" (using | instead of /) it doesn't add the layer to the map but a window pops up with a list of all the feature classes in the gdb, I choose the one I want and it gets added. Any ideas why this behaviour? Commented Feb 8, 2019 at 14:39

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.