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...")
If you throw some print statements in there, at least after the first line, does anything get printed (perhaps lyr.name as an example
Dan, I removed the print statements that I had and replaced them with arcpy.AddMessage("some text...") statements to shoot out messages as the tool runs. Tried running w/print statements initially but didn't see messages come up anywhere.
well use addmessage then and check the output messages from the tool's run
The basic issue is that in the interactive window the layers are recognised as objects and the underlying source is found. When in a script the layers are not visible because you are not in ArcGIS. You have to use the path to the featureclass.
If you still want to use layer names, you could save the layer to a lyrx file and read that in as a layer object which has a property to find the featureclass path.
In the script tool you could add parameters that ask for a 'layer name'. Then you would have to guess the featureclass is the same as the layer. But if you ask for a layerobject then you will get an object that knows the source featureclass.
Tip: when debugging don't use the try/except error trapping. Just let it crash and you will get better feedback on where and why it has failed. If you do add the trap, then print what the error was.
In a standalone script to make a layer you have to 1. Define the featureclass and path, 2. my_layer = arcpy.MakeFeatureLayer(...). You can create a filter, select by location and all the other fancy things that a layer makes possible without making a copy of the featureclass. Then the layer name can be used like your interactive script.
Adding print statements only show when you run the script from an editor. A good thing to do because you will trap all the syntax errors earlier and you can add break points and print intermediate results. I use arcpy.management.GetCount() a lot to stop if there are no records selected and arcpy.Exists(fc) to see if I have got the data.