1

Running ArcGIS Pro 2.2, I have a layer in ~20 maps that needs it's name changed. I pulled this code together from a bunch of snippets, and have done my best to make sure it's updated appropriately, but I've hit a point where it runs without an error, but there are no changes afterwards.

What am I missing here?

import arcpy
aprx = arcpy.mp.ArcGISProject(r"C:\GIS_Local\Working.aprx")
for m in aprx.listMaps():
  layers = m.listLayers()
  for lyr in layers:
    if lyr.name == "old name":
      lyr.name = "new name"

There's a similar Q&A from a couple years ago - Use ArcPy to rename MXD layers, but I haven't been able to get the code there to update to Python 3.0 correctly, so I pulled this from the samples on the ESRI help and this GeoNet post

2 Answers 2

2

You need to save your project to persist the changes that you have made to layer names in its maps.

Add this at the end with no indent:

aprx.save()
0

I had the exact same question and still was having no luck renaming my layers inside ArcGIS Pro even after adding aprx.save() to the end of the script.

After some digging I came across these guidelines for arcpy.mp

The second technique is to use the CURRENT keyword as an input parameter to the ArcGISProject function. This technique only works from within the application because the ArcGISProject object references the project that is currently loaded into the application.

I changed my .aprx path to 'current' and the script worked great.

aprx = arcpy.mp.ArcGISProject('current')

m = aprx.listMaps()[0]
for lyr in m.listLayers():
    if lyr.name == 'old name':
        lyr.name = 'new name'
aprx.save()

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.