Select to view content in your preferred language

Residual locks on aprx files after list iteration.

144
2
04-03-2025 08:18 AM
beachcomber_0972
Emerging Contributor

I'm having an odd issue with residual locks on aprx files when I interact with them via iteration through a list of names. Specifically, the first file that I reference ends up with a hanging lock but the others do not.

I've queried with locals(), and Python does not think it has any hanging references left to an open arcpy object. Here is the code (with paths and files anonymized):

objDict = locals().copy()
prjPath ="C:/Pydata/thisproject/testdir/"
prjs = ["project1.aprx", "project2.aprx","project3.aprx", "project4.aprx"]
shpPath = "C:/Pydata/thisproject/subproject/outputs/"
boundaryPath = "C:/Pydata/thisproject/GIScovs/boundaryshapefile/"

mths = ["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"]
stages = ["C4","C3","C2","C1"]
for idx, prjName in enumerate(prjs):
     fullname = prjPath + prjName
     ArcProject = arcpy.mp.ArcGISProject(fullname) #open the required project
     workingmap = ArcProject.listMaps()[0] # open map to add shapefiles
     stg = stages[idx] #get the relevant stage
     #by iterating the month list in reverse order, placement within the project aligns with desired pattern
     for mth in reversed(mths):
          shpStr = stg + "_" + mth + "_val"
          shpName = shpPath + shpStr
          if arcpy.Exists(shpName): # make sure the desired file exists before adding in order to trap errors related to sparse data
               workingmap.addDataFromPath(shpName)
           else:
               print("shape " + shpName + " not found")

     #next add the desired boundary file:
     #this is the name of the desired layer
     boundaryLyrPath = "C:/Pydata/GIScovs/boundary.lyrx"
     boundaryLyr = arcpy.mp.LayerFile(boundaryLyrPath)
     workingmap.addLayer(boundaryLyr)
     #save the project
     ArcProject.save()
     
     #DELETE ALL REFERENCES TO ARCPY OBJECTS
     del boundaryLyr
     del workingmap
     del ArcProject
newObjDict = locals().copy()
for listitem in newObjDict:
#print active objects added by this code block
if listitem not in objDict:
     print(listitem)

 

The printout from comparing the dictionaries gives:

prjPath
prjs
shpPath boundaryPath mths stages idx stg mth shpStr shpName boundaryLyrPath

All of these are pure Python objects, none are ArcPy references.

When I run this, "project1.aprx" (the first item in the projects list) ends up with a hanging lock such that in the next block of code that I run it cannot be saved (it's read-only). The others do not have the same issue. I'm at a loss, but maybe I'm missing something obvious? 

Thanks in advance for any thoughts.

0 Kudos
2 Replies
DanPatterson
MVP Esteemed Contributor

a related thread to locks and arcpy.mapping which may be of interest

arcpy.mp.ArcGISProject .aprx lock issue

I tracked down the enhancement request

ENH-000121601 for ArcGIS Pro

apparently "in Product Plan"

and a related "Idea" with a protracted discussion or python and aprx stuff

Save Project As, Creating more than just the APRX - Esri Community


... sort of retired...
0 Kudos
beachcomber_0972
Emerging Contributor

Thanks for the info. I'd know ArcPy locks sometimes got a little wonky, I just don't know how to get around this particular one. I'm not quite sure how this got marked as 'solved' because it's still not.

Anyhow, I selectively commented out actions in the script I posted, and the issue arises from lines 18 and 19- either from Arcpy.exists or (more likely) workingmap.addDataFromPath. I don't see any easy way around using those two commands to do what I want, though.

0 Kudos