I am having trouble getting my definition query to be defined. What am I missing? The current error is:

Runtime error Traceback (most recent call last): File "<string>", line 44, in <module> NameError: name 'df' is not defined

# Import arcpy module
import arcpy
import os

# Local variables:
plain_radius5Mi_lyr = "Filepath\\plain_radius5Mi.lyr"
hike__2_ = plain_radius5Mi_lyr
XYplain2__2_ = "XYplain2"
XYplain3 = "Filepath\\plain.gdb\\XYplain3"
arcpy.env.overwriteOutput = True
hike = "Filepath\\plain.gdb\\hike"

# Process: Select
arcpy.Select_analysis(XYplain2__2_, XYplain3, "")

# Process: Multiple Ring Buffer
arcpy.MultipleRingBuffer_analysis(XYplain3, hike, "5;10;15;20;25;35;50", "NauticalMiles", "distance", "NONE", "FULL")

# Process: Apply Symbology From Layer
arcpy.ApplySymbologyFromLayer_management("hike", plain_radius5Mi_lyr)
# top level directory where all the map docs are
path = r"<path>" 

# recursively search the directory for .mxd files
def getMXD(path):
    for dirpath, dirnames, filenames in os.walk(path):
        for f in filenames:
            if f.endswith(".mxd"):
                yield os.path.join(dirpath, f)

for mapdoc in getMXD(path):
    # might need to change wildcarding here
    mxd = arcpy.mapping.MapDocument(mapdoc) 
    df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
for lyr in arcpy.mapping.ListLayers(hike, "*", df):        
     if lyr.supports("DEFINITIONQUERY"):
          lyr.definitionQuery = "ID = '35'"
  • That error seems incomplete and does not seem to come from the precise code that you have presented.
    – PolyGeo
    Jun 16, 2017 at 12:34
  • 2
    df is out of scope in the second loop. If you want to retain it, you should initialize it before the first loop (and verify that it was set in the first loop)
    – Vince
    Jun 16, 2017 at 12:42
  • 2
    @Vince I thought Python was function scoped, not blocked scoped, so df would have a value if the loop ran at least once. Maybe that was what you meant. In general, the code is odd as the loop would only df to the last value in getMXD anyway; seems like a indent-format issue. Jun 16, 2017 at 15:38

1 Answer 1


This error would occur if your function getMXD returns nothing. To be sure, I'd print out the mxd name each time one is found. You could then do something like this to check that getMXD returns something:

#variable to indicate whether or not an mxd is found
found = False
for mapdoc in getMXD(path):
    # set variable to true when mxd is found
    found = True
    #print mxd name, indicating an mxd has been found
    print os.path.basename (mapdoc)
    mxd = arcpy.mapping.MapDocument(mapdoc) 
    df = arcpy.mapping.ListDataFrames(mxd, "*")[0]

#check that mxd is found
if found:
    for lyr in arcpy.mapping.ListLayers(hike, "*", df):        
         if lyr.supports("DEFINITIONQUERY"):
              lyr.definitionQuery = "ID = '35'"
  • A Boolean is unnecessary if you init the df variable to None. the you can test the variable itself (if df:)
    – Vince
    Jun 17, 2017 at 0:02

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.