Hello,
I wrote a Python script that works when I run it in the ArcGIS Pro Python window (with hardcoded inputs) but not as a script tool. The point of failure in the script tool is when selecting by location. When it gets to that point, nothing gets selected and the script continues to run without errors. Below is a section of code where nothing is getting selected in the tool but it works perfectly when the script is run from the Python window sans user inputs. Is there something that I need to do to format this code in order for select by location to work in a script tool?
for lyr in mp_lyrs:
if lyr.isGroupLayer and lyr.name == "Layers":
lyrs = lyr.listLayers() #List layers within the Layers group layer.
for l in lyrs:
l_path = os.path.join(scratchDB + "\\" +l.name) #output feature class, including file path
fc_out = l_path.replace('(', '').replace('-', '_').replace(' ', '_').replace(')','') #reformatted output feature class, including file path
if l.isGroupLayer:
continue
elif l.name =="Map Index100":
continue
try:
print(l.name)
fcount = arcpy.GetCount_management(l)
with arcpy.EnvManager(extent="-9484369.58533597 4188374.56590133 -9151819.94355486 4323557.34711317"):
arcpy.management.SelectLayerByLocation(l, "INTERSECT", polyindx, None, "ADD_TO_SELECTION", "NOT_INVERT")
arcpy.AddMessage("l.name" + " are selected")
result = arcpy.GetCount_management(l)
arcpy.AddMessage("Features selected: " + str(result))
selection = int(str(result))
if selection == 0:
continue
else:
arcpy.AddMessage("Clipping features")
with arcpy.EnvManager(extent="-9484369.58533597 4188374.56590133 -9151819.94355486 4323557.34711317"):
arcpy.analysis.Clip(l, polyindx, fc_out, None)
value_table.addrRow(fc_out)
except:
arcpy.AddMessage("An exception has occurred...")