It seems that arcpy.mp does not release the lock of the resulting (new) aprx document. The following code does create a new aprx file:
aprx_project.saveACopy(os.path.join(workspace, prefix, name_to_save)) del aprx_project
Unfortunately, the lock persists on the new .aprx file. Is there any way to remove it? Only after I close the ArcGIS Pro which I use to run the script, the lock is released.
Any hints are much appreciated!
ArcGIS Pro v.2.2.2
# ---- try the hardway aprx_project.saveACopy(os.path.join(workspace, prefix, name_to_save)) workspace = None prefix = None name_to_save = None del prefix del workspace del name_to_save del aprx_project #---- still may fail to release the lock #---- close your ide
are you running this in the internal python ide or an external one?
I suppose you have tried deleting the workspace, prefix and name_to_save prior to deleting the project variable as well.
finally, try closing the python window. 'del' removes the variable from the namespace only, locks can be different beast
I am running the script from ArcGIS Pro (Script tool in Toolbox), so there is no python window or ide to close.
Indeed, I tried to delete those variables as well.
The documentation clearly states that the object still has the reference to the former aprx file:
The method creates a new output project file, but the project variable continues to reference the original ArcGISProject object. It does not duplicate all the content within a project folder.
So, I reckon it is a bug in arcpy....
When you run it from the tool in arctoolbox and it completes, do you notice an orange exclamation mark beside the output filename, with a warning that the output filename already exists? If so, this would mean that references to the output filename persist. If not, then the deletion of the output filename is no longer in memory.. Worth a check
I've run into the same problem - very simple scenario and there seems to be nothing I can do in my python script to delete the aprx file that I've finished with. Restarting the python process gets rid of the file but that isn't really an option in my workflow.
I have a script that copies an aprx file to a temporary directory. The script does it's thing but needs to remove the temporary directory when it's done working. I couldn't remove the temp directory b/c of the aprx lock.
I was able to get around it by using the built-in python module atexit. atexit allows you to register a function to run at the end of a script... this may not help you if you're running code from an interactive window. However, for a standalone script this worked for me. https://docs.python.org/3.7/library/atexit.html
def goodbye(name, adjective): print('Goodbye %s, it was %s to meet you.' % (name, adjective)) import atexit atexit.register(goodbye, 'Donny', 'nice') # or: atexit.register(goodbye, adjective='nice', name='Donny')