<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic SaveToLayerFile() generates invalid .lyrx from stand-alone Python script in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/savetolayerfile-generates-invalid-lyrx-from-stand/m-p/1654706#M99363</link>
    <description>&lt;P&gt;My stand-alone arcpy script below produces .lyrx files in the right location, with the right name.&lt;/P&gt;&lt;P&gt;If I drag any of the .lyrx files onto a map in an ArcGIS Pro project, it fails with the error:&amp;nbsp; "&lt;STRONG&gt;The selected layer file is not valid&lt;/STRONG&gt;".&amp;nbsp; The contencts of the .lyrx file (viewed in a text editor) is:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;{
  "type" : "CIMLayerDocument",
  "version" : "3.5.0",
  "build" : 57366
}&lt;/LI-CODE&gt;&lt;P&gt;This is clearly not a valid .lyrx file!&lt;/P&gt;&lt;P&gt;This issue occurs if I run the script against a wide variety of ArcGIS Pro projects, using ArcGIS Pro version 3.5.2.&lt;/P&gt;&lt;P&gt;If I right-click on the same layer(s) in the same ArcGIS Pro project and save to a layer file that way, then the .lyrx can be used without any problems.&lt;/P&gt;&lt;P&gt;Similarly, this same code works fine if running from an ArcGIS Pro python window.&amp;nbsp; It only produces the invalid .lyrx files when running from a stand-alone Python script.&lt;/P&gt;&lt;P&gt;What is going wrong here?&lt;/P&gt;&lt;P&gt;Here is the very simple script:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;for lyr in mp.listLayers():
    lyrx_path = os.path.join(layers_dir, lyr.name) + ".lyrx"
    arcpy.SaveToLayerFile_management(lyr, lyrx_path)&lt;/LI-CODE&gt;&lt;P&gt;&lt;STRONG&gt;UPDATE:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;ESRI have been able to reproduce this issue and they have suggested that I could try using arcpy.management.MakeFeatureLayer() to create a feature layer from the Layer object and then saving that to a layer file instead.&amp;nbsp; However, this suggestion is unsuitable for my situation, so I haven't even tried it.&amp;nbsp; I need this to work for raster layers, online tile layers, and most especially, I need this to work for group layers (which include multiple other layers).&amp;nbsp; These cannot be converted to feature layers!&lt;/P&gt;</description>
    <pubDate>Thu, 02 Oct 2025 22:48:48 GMT</pubDate>
    <dc:creator>GISDataTasPorts</dc:creator>
    <dc:date>2025-10-02T22:48:48Z</dc:date>
    <item>
      <title>SaveToLayerFile() generates invalid .lyrx from stand-alone Python script</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/savetolayerfile-generates-invalid-lyrx-from-stand/m-p/1654706#M99363</link>
      <description>&lt;P&gt;My stand-alone arcpy script below produces .lyrx files in the right location, with the right name.&lt;/P&gt;&lt;P&gt;If I drag any of the .lyrx files onto a map in an ArcGIS Pro project, it fails with the error:&amp;nbsp; "&lt;STRONG&gt;The selected layer file is not valid&lt;/STRONG&gt;".&amp;nbsp; The contencts of the .lyrx file (viewed in a text editor) is:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;{
  "type" : "CIMLayerDocument",
  "version" : "3.5.0",
  "build" : 57366
}&lt;/LI-CODE&gt;&lt;P&gt;This is clearly not a valid .lyrx file!&lt;/P&gt;&lt;P&gt;This issue occurs if I run the script against a wide variety of ArcGIS Pro projects, using ArcGIS Pro version 3.5.2.&lt;/P&gt;&lt;P&gt;If I right-click on the same layer(s) in the same ArcGIS Pro project and save to a layer file that way, then the .lyrx can be used without any problems.&lt;/P&gt;&lt;P&gt;Similarly, this same code works fine if running from an ArcGIS Pro python window.&amp;nbsp; It only produces the invalid .lyrx files when running from a stand-alone Python script.&lt;/P&gt;&lt;P&gt;What is going wrong here?&lt;/P&gt;&lt;P&gt;Here is the very simple script:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;for lyr in mp.listLayers():
    lyrx_path = os.path.join(layers_dir, lyr.name) + ".lyrx"
    arcpy.SaveToLayerFile_management(lyr, lyrx_path)&lt;/LI-CODE&gt;&lt;P&gt;&lt;STRONG&gt;UPDATE:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;ESRI have been able to reproduce this issue and they have suggested that I could try using arcpy.management.MakeFeatureLayer() to create a feature layer from the Layer object and then saving that to a layer file instead.&amp;nbsp; However, this suggestion is unsuitable for my situation, so I haven't even tried it.&amp;nbsp; I need this to work for raster layers, online tile layers, and most especially, I need this to work for group layers (which include multiple other layers).&amp;nbsp; These cannot be converted to feature layers!&lt;/P&gt;</description>
      <pubDate>Thu, 02 Oct 2025 22:48:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/savetolayerfile-generates-invalid-lyrx-from-stand/m-p/1654706#M99363</guid>
      <dc:creator>GISDataTasPorts</dc:creator>
      <dc:date>2025-10-02T22:48:48Z</dc:date>
    </item>
    <item>
      <title>Re: SaveToLayerFile() generates invalid .lyrx from stand-alone Python script</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/savetolayerfile-generates-invalid-lyrx-from-stand/m-p/1655043#M99381</link>
      <description>&lt;P&gt;I have found a work-around for this issue…&lt;/P&gt;&lt;P&gt;Instead of doing this:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;arcpy.SaveToLayerFile_management(group_layer, lyrx_path)&lt;/LI-CODE&gt;&lt;P&gt;I can do this:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;group_layer.saveACopy(lyrx_path)&lt;/LI-CODE&gt;&lt;P&gt;The first one fails (produces invalid .lyrx file) when run in a stand-alone Python script (although curiously it works when run in an ArcGIS Python window).&amp;nbsp; NB:&amp;nbsp; This failure does NOT produce any error messages. &amp;nbsp;&amp;nbsp;It fails silently, and therefore I have asked ESRI to consider logging this as a bug.&amp;nbsp; (I believe that it should, at the very least, produce an error message, or otherwise succeed.&amp;nbsp; It should not fail silently!)&lt;/P&gt;&lt;P&gt;The second one succeeds in a stand-alone Python script and produces a valid .lyrx file.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;UPDATE:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;This has now been logged as a bug by ESRI:&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;BUG-000179880&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Oct 2025 02:57:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/savetolayerfile-generates-invalid-lyrx-from-stand/m-p/1655043#M99381</guid>
      <dc:creator>GISDataTasPorts</dc:creator>
      <dc:date>2025-10-03T02:57:23Z</dc:date>
    </item>
  </channel>
</rss>

