Select to view content in your preferred language

arcpy SaveToLayerFile creates invalid file

1079
8
10-24-2022 09:39 AM
Labels (1)
by Anonymous User
Not applicable

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

0 Kudos
8 Replies
JonathanNeal
Esri Contributor

@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

0 Kudos
DanPatterson
MVP Esteemed Contributor

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


... sort of retired...
0 Kudos
by Anonymous User
Not applicable

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.

0 Kudos
JonathanNeal
Esri Contributor

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)

0 Kudos
RhettZufelt
MVP Notable Contributor

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_

0 Kudos
by Anonymous User
Not applicable

The path and feature class are valid. They work fine with other commands.

0 Kudos
DanPatterson
MVP Esteemed Contributor

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


... sort of retired...
0 Kudos
by Anonymous User
Not applicable

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.

0 Kudos