I'm attempting to run a simple tool script in ArcGIS Pro 2.4 that creates a feature layer from an existing layer. The script creates the feature layer just fine if running the script in PyCharm, as well as the Python interpreter within ArcGIS (adds visible feature layer under Maps).

Why would the layer not be created and be visible in ArcGIS pro if running the script as a tool, compared to running it externally or in the interpreter?

Anyone else having this issue with 2.4 when running scripts as a tool?

No errors.

p = arcpy.mp.ArcGISProject("CURRENT")
a_map = p.activeMap
lyrs = a_map.listLayers()
# Checking layers within activeMap, they exist
for l in lyrs:
 arcpy.AddMessage("{0}: activeMap layer: ".format(l))

# Feature layer not created when used as a tool, but works in the Python interpreter & external PyCharm run
arcpy.MakeFeatureLayer_management(a_map.listLayers('countries')[0], 'test')

Determined the problem for anyone else running into this issue. I just needed to return the new layer as output, then add that layer to the map. I discovered this by finding a setting selected in ->Project->Options->Geoprocessing "Add output datasets to open map".

out_lyr = a_map.listLayers('countries')[0]
new_lyr = arcpy.MakeFeatureLayer_management(out_lyr, 'tester').getOutput(0)

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.