I'm using ArcGIS Pro 3.02 and Python 3.9 to create a feature layer and save the layer to a lyrx file. This generates an invalid file error message ("The selected layer file is not valid") when I attempt to add the lyrx to a map. Results are the same whether I run it as a stand-alone script or from the Python window in Pro.
This is the code:
# input polygon feature class
infeatures = "E:\\_Projects\\sample.gdb\\test"
maplayer = 'test'
layerfile = "E:\\Layers\\Test.lyrx"
arcpy.management.MakeFeatureLayer(infeatures, maplayer)
arcpy.management.SaveToLayerFile(maplayer, layerfile, 'ABSOLUTE')
Thank you,
Pam
@Anonymous User Checked in 3.1 (this is not an issue). Are you able to share an empty table. (Maybe there is something special in you data?)
To create an empty feature:
1. Select Layer By Attribute where ObjectId = -1
2. Copy Features
in immediate mode, did you try
infeatures = "E:\\_Projects\\sample.gdb\\test"
layerfile = "E:\\Layers\\Test.lyrx"
arcpy.management.SaveToLayerFile(infeatures, layerfile, 'ABSOLUTE')
Save To Layer File (Data Management)—ArcGIS Pro | Documentation
Hmm. The script works in immediate mode (iPython). I still get an 'invalid file' when loading the same code in the Python window and running it from there.
Also, checked in 3.0 no issue with the above script. (Would be insiteful too if you did get the path to work and not the layerName)
your exact code is working fine in Pro 2.9.3 for me as long as E:\\_Projects\\sample.gdb\\test actually exists and is valid.
R_
The path and feature class are valid. They work fine with other commands.
Sounds strangely like
BUG-000117445: The Save To Layer File management tool fails and ret.. (esri.com)
which you seem to have tried the workaround
Thanks Dan. I'm currently dead in the water for running any of my scripts with Pro because I can't resolve the 'no module named arcpy' error that pops up every week.