This is based on a previous question/answer that I had. Replacing values in multiple columns using QGIS Field Calculator?

I need it to update 3 fields now and I get an name 'e' is not defined error. What have I done wrong?

layer = qgis.utils.iface.activeLayer()

# Set field names you want to update
field_1 = "New_Name"
field_2 = "New_File_P"
field_3 = "Process"
idx_1 = layer.fieldNameIndex( field_1 )
idx_2 = layer.fieldNameIndex( field_2 )
idx_3 = layer.fieldNameIndex( field_3 )

# Set expression to find all features which fall into expression
exp = QgsExpression( """ "Existing_F" LIKE '%2015 Aerial Ortho\\_2015\\ECW_Tiles%'  """ )

# Select all features which fall into expression
ids = [i.id() for i in layer.getFeatures(QgsFeatureRequest(exp))]

# Set expressions to fill in values for selected features
formula_1 = """ "File_Name" """
formula_2 = """ '\\sipv-gis01\\GIS_Library\\Raster\\Aerial\\2015_25cm\\Images\\ECW' """
formula_3 = """  'y'  """
e_1 = QgsExpression(formula_1)
e_2 = QgsExpression(formula_2)
e_3 = QgsExpression(formula_3)

with edit(layer):
    # For each selected feature
    for f in layer.selectedFeatures():
        f[idx_1] = e_1.evaluate(f)
        f[idx_2] = e_2.evaluate(f)
        f[idx_3] = e_3.evaluate(f)



Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'e' is not defined
  • 1
    I don't know python, but what stands 'e' for? don't see any declaration in your code snippet or is it al global variable? – Ms. Smith May 13 '16 at 7:03
  • Is it a typo for the exp QgsExpression you declared in line ~20? – Spacedman May 13 '16 at 7:09
  • 1
    Sorry, that was a mistake on my part. You shouldn't need that line e.prepare(layer.pendingFields()). – Joseph May 13 '16 at 10:03
  • 1
    Thanks George but @JochenSchwarze posted an answer which already describes commenting out (or removing) that line so please accept his :) – Joseph May 17 '16 at 9:23
  • 1
    @GeorgeC - Struggling to find the problem as the code still works and updates the .dbf file. This is after I clear all attributes in the 3 fields you mention in your code and setting the expression to exp = QgsExpression( """ "File_Type" = 'shp' """ ). Don't think I can help much further, sorry -_- – Joseph May 24 '16 at 11:07

According to http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/expressions.html the member function QgsExpression.prepare() is not mandatory but makes the execution faster. I would recommend to comment out the line e.prepare(layer.pendingFields()) and see what happens.

And as far as I see, an exp.prepare(...) is missing directly behind the creation of the expression (i.e. exp=QgsExpression("""...) if things run slow, because the expression is evaluated in the next line as an argument to QgsFeatureRequest(exp).

| improve this answer | |
  • I approved this to quick. It appears to work (i.e no errors) but the data fields are not updated. The query works correctly when just run in "select by expression" but not in the code. What else could be wrong? – GeorgeC May 19 '16 at 22:23
  • Hello everyone, does this work with QGIS 3.x and python 3? I failed to repeat this successfully. – Vitruvius Sep 18 '18 at 7:53

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.