2

This is the piece of code that is failing:

##nomenclatura = string '1.1.E.3.52'
##pdfOutput = output string
from qgis.core import QgsProject
project = QgsProject.instance()
QgsExpressionContextUtils.setProjectVariable(project,'Manzana_actual',nomenclatura)
tx = pScope.variable('Manzana_actual')
layer = project.instance().mapLayersByName('Mzs,Qt,Chs')[0]
iface.setActiveLayer(layer)
#continues#

ERROR MESSAGE:

QgsExpressionContextUtils.setProjectVariable(project,'Manzana_actual',nomenclatura)

NameError: name 'QgsExpressionContextUtils' is not defined

I copy pasted all the code in the python console and it works, but fails when I try to save it as a script...

1

You need to import QgsExpressionContextUtils as it is part of qgis.core. So you can include it in your imports:

from qgis.core import QgsExpressionContextUtils, QgsProject

Most modules are loaded for you to use in the Python Console. But when using scripts, you will need to manually load the modules yourself which would explain the error you received.

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.