QGIS Python – Error Using Python Console in QGIS

pyqgispythonqgis

I am using QGIS version 3.2.2-Bonn on Windows 10 and this is my first attempt using Python on QGIS from the console. I am following this article:
Getting Started With Python Programming

When typing iface in the console a receive the following reply:

qgis._gui.QgisInterface object at 0x0000019EF2194D38

Then when I try to use the dir(layer) command I receive the following error message:

*dir(layer)*
Traceback (most recent call last):
  File "C:\PROGRA~1\QGIS3~1.2\apps\Python36\lib\code.py", line 91, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
NameError: name 'layer' is not defined

Do I need to configure something before starting using the console?

Best Answer

  • iface is predefined variable in QGIS. qgis._gui.QgisInterface object at .... means that variable iface is an instance of QgisInterface.

  • You probably didn't define variable layer. Thus, you get an error. In the article there is a line layer = iface.activeLayer() above the dir(layer). Firstly, run that code (please be sure to select a layer before), then you can use dir(layer).

Related Question