Hi,
I've encountered weird behaviour in the saveACopy method of the arcpy.mp.ArcGISProject class. It seems that it modifies the value of "CURRENT". I've assembled a short code sample:
import arcpy
def PrintFilePathOfCurrentArcGISProject(msg):
# Open the "CURRENT" project and print the file path
aprx = arcpy.mp.ArcGISProject("CURRENT")
arcpy.AddMessage(f"{msg}: {aprx.filePath}")
def main():
PrintFilePathOfCurrentArcGISProject("Before opening \"CURRENT\" aprx")
aprx = arcpy.mp.ArcGISProject("CURRENT")
aprx.saveACopy(r"D:\test_copy.aprx")
PrintFilePathOfCurrentArcGISProject("After saveACopy")
if __name__ == "__main__":
main()
When running the script in ArcGIS Pro 2.9.3, the output is:
Before opening "CURRENT" aprx: C:\Users\Madlener\Documents\ArcGIS\Projects\MyProject\MyProject.aprx
After saveACopy: D:\test_copy.aprx
I wonder if this is a bug (I certainly think it is one..) If it's not a bug, this behaviour should be documented in the saveACopy method. The problem is that I cannot restore the old value of "CURRENT" by any means. If there's a way though, I'd be glad if somebody could let me know.
Thanks in advance
Christian
why not reopen the original one again
Of course I can store the inital value of aprx.filePath in a variable and reopen the project again later but my scenario is a bit different:
My Script Tool basically creates a copy of the currently opened ArcGIS Pro project to a user defined location and updates some layer connection settings. Now that saveACopy modifies the value of "CURRENT", the script obviously can't be run more than one time because the second time it's run, "CURRENT" points to the copy and not to the currently opened Project anymore.
Now maybe (I haven't checked) there's the possibility of having a global variable that is persistent across multiple tool executions so I can set it the very first time.