Select to view content in your preferred language

project_service_aprx_object.save() Error from Pro 2.8.x to 2.9 upgrade

11785
25
Jump to solution
11-23-2021 02:26 PM
Min-DongChang
Occasional Contributor

I have a code that a developer created that no long works for me.  With a recent upgrade from 2.8.x to 2.9, the script no errors out at a line " project_service_aprx_object.save()".  What I believe the code is trying to do update the source of feature layers in a map ("1_Service_Status_On").  However, when it comes to the  project_service_aprx_object.save() line, it gives me the following error:

Traceback (most recent call last):
File "<string>", line 686, in <module>
File "A:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 285, in save
return convertArcObjectToPythonObject(self._arc_object.save(*gp_fixargs((), True)))
OSError: G:\SDE_User_Connections\Projects\UnitedStates\US_Test01\Web_Services\Test01_Project_Services.aprx

I'm running the code in an empty APRX file so I don't even have the Test01_Project_Services.aprx open.  I'm a novice at python so any I can provide more of the code if necessary.

0 Kudos
25 Replies
RahulGupta2
New Contributor

I encountered the same problem. To resolve the issue I used the following code.

#Remove existing layer from original template
Original_Template = r"C:\TempScript\original.aprx"
aprx = arcpy.mp.ArcGISProject(Original_Template)
aprxMap = aprx.listMaps("Map")
for lyr in aprxMap[0].listLayers():
      aprxMap[0].removeLayer(lyr)

#Add new layer to original template
_layer = arcpy.MakeFeatureLayer_management(fc,"LayerName").getOutput(0)
aprxMap[0].addLayer(_layer,"BOTTOM")
aprx.saveACopy(r"C:\TempScript\Temp_Template.aprx")

#Save original template to the temp template as aprx.save() is not working
aprx_tmp = arcpy.mp.ArcGISProject(r"C:\TempScript\Temp_Template.aprx")
aprxMap_tmp = aprx_tmp.listMaps("Map")
m = aprxMap_tmp[0]
sddraft = m.getWebLayerSharingDraft("FEDERATED_SERVER", "MAP_IMAGE", "LayerName")

Regards

Rahul

0 Kudos
VincentLantaca
Occasional Contributor

seems like there are still issues with aprx.save() even in 3.1, I can run a geoprocessing script with modifies then saves an .aprx file once after opening ArcGIS Pro, but the same script a second time will give the OSError until I re-open Pro

ASw93
by
Regular Contributor

Can confirm I am seeing this same error still in 3.1. 

JaredPilbeam2
MVP Alum

Still happening in 3.3.

0 Kudos
TaylorCarnell1
Occasional Contributor

This happens when rerunning a script because you are getting a new reference to the project and subsequent references are read only. 
It isn't desirable, but is technically documented behaviour. As per ArcGISProject:

Projects can be referenced multiple times, but only the first reference can be saved directly because the other references will be opened as read-only.

You can solve this using:

try:
    project
except NameError:
    project = arcpy.mp.ArcGISProject(project_path)

If you already have multiple references, your memory will need to be cleared first (kernel or machine restart), before this will help.

It's still possible to get orphaned references in certain conditions, so this isn't bullet proof.

 

0 Kudos
GISDepartmentMFM
Emerging Contributor

Is there a way to do this without the data redundancy of that comes from save a copy? This feels undesirable if you have a large project.

0 Kudos