A part of my script where I extract the view definition is returning an empty string when I run it, but if I run the same exact code in ArcGIS, I get the correct return.

It is only for the desc.whereClause return, the others such as desc.name, desc.nameString are working correctly.

Anyone know what could be causing this?

mxdpath = r"C:\...\blah.mxd"
mapDoc = arcpy.mapping.MapDocument(mxdpath)
dataFrames = arcpy.mapping.ListDataFrames(mapDoc)
for i in range(0,len(dataFrames)):
    lyrs = arcpy.mapping.ListLayers(mapDoc,"",dataFrames[i])
    for j in range(0,len(lyrs)):
        if lyrs[j].isFeatureLayer:
            desc = arcpy.Describe(lyrs[j])
            print "Name string:  " + desc.nameString
            print "Where Clause:  " + desc.whereClause

This code returns a blank u'' for desc.whereClause, however the same code will produce something like 'objectId = 1' if I run it in the python app inside ArcMap Desktop

  • When executing from the python Window in ArcMap, are you using mxdpath = 'CURRENT'?
    – Paul
    Commented Nov 16, 2016 at 23:23
  • import arcpy? Have you already?
    – jbalk
    Commented Nov 17, 2016 at 4:33
  • Try lyrs[j].definitionQuery to get the where clause.
    – jbalk
    Commented Nov 17, 2016 at 4:36

1 Answer 1


Try this instead(last section of your code):

if lyrs[j].isFeatureLayer:
    print lyrs[j].name
    print lyrs[j].definitionQuery

Also import arcpy at the beginning if you haven't yet.

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.