I'm developing a python app that makes heavy use of the ArcGIS API for Python and running into a problem where it appears that when I run exportToSDDraft, a lock is left on the aprx file that I used to create the draft. This causes a failure downstream when I try to reopen the file (or delete it). Any suggestions on how to release this lock or is there an alternative to exportToSDDraft (Python 3).
Here is the error message I get when I try to delete the file is os.remove. I confirmed that it is actually the same process that is holding the lock - not a different process as the message states.
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'E:\\ROW_as_habitat\\projects\\test.aprx'
Here is the error when I do the delete with arcpy.DeleteManagement
ExecuteError: ERROR 000601: Cannot delete E:\ROW_as_habitat\projects\test.aprx. May be locked by another application.
Failed to execute (Delete).
I boiled down the script as far as I could to recreate the problem. When I remove the line with exportToSDDraft, there is no lock on the aprx file and the script completes successfully.
blank_project_fn = os.path.abspath('config/templates/blank_project.aprx')
project_fn = os.path.normpath('E:/ROW_as_habitat/projects/test.aprx')
sd_draft_fn = os.path.normpath('E:/ROW_as_habitat/sd_drafts/test.sddraft')
connection_file = os.path.normpath('E:/ROW_as_habitat/conn3.ags')
layer_fn = os.path.normpath('E:/ROW_as_habitat/org_layer_files/Ameren_Sites.lyrx')
def create_project ():
# Make a copy of the blank temlate project
template_aprx = arcpy.mp.ArcGISProject(blank_project_fn)
# Open the new copy and make changes
aprx_obj = arcpy.mp.ArcGISProject(project_fn)
m_map = aprx_obj.listMaps()
def create_sd_draft ():
aprx_obj = arcpy.mp.ArcGISProject(project_fn) # THIS IS THE FILE THE STAYS LOCKED
aprx_map = aprx_obj.listMaps()
# Create MapServiceDraft and set service properties
service_draft = arcpy.sharing.CreateSharingDraft("STANDALONE_SERVER", "MAP_SERVICE", 'test', aprx_map)
service_draft.targetServer = connection_file
service_draft.exportToSDDraft(sd_draft_fn) # THIS APPEARS TO LEAVE THE APRX FILE LOCKED
def delete_project ():
if __name__ == '__main__':
delete_project () # TEST FAILS HERE WHEN exportToSDDRaft is not commented out