I am trying to load a layer from an mxd and back up the layer using ArcGIS Pro and Python.
aprx.importDocument(current_document_name) mlist = aprx.listMaps() infra_layers = mlist[0].listLayers() new_file_name = backup_geodatabase_path + 'ActiveInfraProjects.lyrx' arcpy.SaveToLayerFile_management(infra_layers[0].name, new_file_name, 'ABSOLUTE')
#I get the following error: ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Input Layer: Dataset active_infrastructure_projects does not exist or is not supported Failed to execute (SaveToLayerFile). #It says it doesn't exist but I was able to get the name from the file (infra_layers[0]) so it must exist. #Why is it not supported?
Solved! Go to Solution.
I needed to convert to a feature layer first. arcpy.MakeFeatureLayer_management(infra_layers[0], "fl") arcpy.CopyFeatures_management('fl', workspace_name) arcpy.SaveToLayerFile_management(fl, new_file_name, 'ABSOLUTE')
I've just tried your code and it worked for me. So this leads me to wonder if the problem is in this line:
aprx.importDocument(current_document_name)
Is this code in a loop and you are only showing us the salient commands? If so may be the loop is loading up a project with say a broken layer in it? Or you are have some sort of network connection issue?
You mention a loop. I am importing a single .mxd so why would it be in a loop?
You are missing all the import modules part of your script which would suggest to me that you are showing a cut down version hence me speculating how you are running this code. You don't state how you are running it. So I'm suggesting you are not showing us the bigger picture and the problem lies elsewhere as I was able to run your code without error.
watch your path construction
# ---- not good
backup_geodatabase_path = r"c:\somefolder\some.gdb"
new_file_name = backup_geodatabase_path + 'ActiveInfraProjects.lyrx'
new_file_name
'c:\\somefolder\\some.gdbActiveInfraProjects.lyrx'
# ---- concatenate the bits simply
args = [backup_geodatabase_path, 'ActiveInfraProjects.lyrx']
new_file_name = "\\".join([a for a in args])
new_file_name
'c:\\somefolder\\some.gdb\\ActiveInfraProjects.lyrx'
Or use os.path.join(var1, var2, varetc) to construct paths to things
I needed to convert to a feature layer first. arcpy.MakeFeatureLayer_management(infra_layers[0], "fl") arcpy.CopyFeatures_management('fl', workspace_name) arcpy.SaveToLayerFile_management(fl, new_file_name, 'ABSOLUTE')