1

I just installed QGIS 3.6, and while the basic Python functions work, I cannot get the most basic GIS related functions working. Every attempt results in the "'builtin_function_or_method' object has no attribute" message. Here is one example:

    print(qgis.utils.iface.activeLayer.name)
Traceback (most recent call last):
  File "C:\OSGEO4~1\apps\Python37\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute 'name'

What could be going wrong here?

  • 4
    try print(qgis.utils.iface.activeLayer().name()) – LaughU Feb 26 at 15:03
  • Where did you get that qgis.utils.iface.activeLayer.name from? – bugmenot123 Feb 26 at 15:16
  • Tried name(),same result. I picked "name" from the code completion list, which seems to be working fine. I get the same results with every example I try, however. For example, the following code fails on "getFeatures()". – chriscalef Feb 26 at 15:35
  • layer = qgis.utils.iface.activeLayer features = layer.getFeatures() – chriscalef Feb 26 at 15:35
  • This is QGIS 3.6 running in windows 8, btw, but I see the same thing on another machine with QGIS 3.4, windows 10. – chriscalef Feb 26 at 15:37
2

You need to call the method to get the layer and then call the name() method on your layer. Something like this should work (as @LaughU says in comment):

print(qgis.utils.iface.activeLayer().name())

And

layer = qgis.utils.iface.activeLayer() # get the active layer
features = layer.getFeatures() # get the QgsFeatureIterator of the layer

Note the () after activeLayer to call this method and get it's return object (a QgsVectorLayer in this case). If you forgot (), you just get a reference to the built-in method activeLayer which doesn't have any attribute called name...

  • AH, thank you very much! Now we're moving again. My ticket has been resolved. :-) – chriscalef Feb 26 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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