I'm seeing some different behavior here in ArcGIS Pro.
In this particular case I'm using
arcpy.SaveToLayerFile_management(in_layer,out_layer)
[Curiously, this works in the python command window but fails in a script tool].
When I try
arcpy.management.SaveToLayerFile(in_layer, out_layer)
in the python window with the same parameters the tool fails with an 000622 error. I tried to supply the other optional parameters but it still failed.
Is ArcMap/ArcGIS Pro open or closed when you do this.
When you say the "python command window" which one?
When you sayt the "python window" the one that is built into *map/*pro?
maybe a 'console' in a python IDE, like the IPython console in Spyder?
can you specify what you used exactly as...
in_layer …. had you set your environments to work in a geodatabase? is *.map/*.pro open
out_layer …. was this a full path to a folder for a layer file (*.lyr or *.lyrx)
I suspect that the in_layer and/or the out_layer can't be found because of the above conditions
Ah, yes...important clarifications. Sorry if i wasn't specific enough...I thought Esri was calling the 'python window' the built-in interface...well, at least that's what they called it in ArcMap. But yes: built-in interface in ArcGIS Pro...no console.
2 separate issues but I'll focus on your questions first.
If I run the the code from the 'python window' the layer file is exported just fine...within the script tool it fails with a 999999 error. One thing to keep in mind here is that these inputs are used elsewhere in the script tool just fine. But let me try to pare this down to what's relevant here.
#Import Modules
import arcpy, os
#Variables
Date = time.strftime("%m%d%Y")
OutputFolder = "D:\\TEMP" ##In the script tool this is exposed as a parameter and not hard-coded.
LayerName = "SoilsMUID_" + Date + ".lyrx"
ExportLayer = os.path.join(OutputFolder, LayerName)
#Setup Map Document Info
aprx = arcpy.mp.ArcGISProject("CURRENT") ##In the script tool, this points to an aprx file using the full path.
map = aprx.listMaps("Soils")[0]
SoilsLyr = map.listLayers('SoilsMUID')[0] ##This layer's data source is a shapefile
#Export Layer File
arcpy.AddMessage("Exporting Layer File to {0}".format(ExportLayer))
arcpy.SaveToLayerFile_management(SoilsLyr, ExportLayer)
In the script tool (if the SaveToLayerFile is commented out), the tool goes on to select the in_layer, zoom to it, and some other stuff and then a PDF is exported no problem.
FWIW, I also tried to create a feature layer from SoilsLyr and then pass that to SaveToLayerFile_management (unneeded but worth a try) and that fails as well.
Layers can be referenced from within a project using the listLayers method on the Map class or in a layer file (.lyr or .lyrx) stored on disk using the listLayers method on the LayerFile class.
Based on that description, I would want to ensure that the SoilsLyr is indeed a full path for a file one disk. change line 15
arcpy.AddMessage("Soils layer {0}\nExporting Layer File to {1}".format(SoilsLyr, ExportLayer))
BTW, you don't need the {0} {1}… ie sequential numbers unless you intend to alter the order... but it is fine, unless you miss a number, then you have to debug … just keep then sequential is easiest
Dan, I didn't expect that it will return a full path for a file on disk since I'm using the listLayers method for a layer within the map. I modified the message per your request and got the following from the script tool.
Again, if we comment out the SaveToLayerFile the script goes on to use SoilsLyr for several other things just fine so it sure seems the script has access to that as an input just fine?
arcpy.SelectLayerByAttribute_management(SoilsLyr, "NEW_SELECTION")
mf.zoomToAllLayers(True,True)
arcpy.SelectLayerByAttribute_management(SoilsLyr, "CLEAR_SELECTION")
mf.camera.setExtent(mf.getLayerExtent(SoilsLyr,True, True))
Can you humor me by trying to save to hardcoded:
ExportLayer =
r'D:\TEMP\SoilsMUID_01172019.lyrx'
I like your thinking in eliminating that, but that also failed with a 999999 error
(Curiously, looking up a 999999 error I expected it to say "This is the most informative error code ever!"...sadly it did not).
Dan Patterson I didn't expect this thread to morph like this so I should probably rename the title.
I'm still curious what the difference between
arcpy.mangement.{tool} vs arcpy.{tool}_management
I would have to look up the reason... someone posted it recently, but the former is 'more pythonic' in that conforms to the way most python packages work
package, module function
ie numpy linalg lstsq …
sometimes (so don't use arcpy as a good example) this represents a folder structure but more generally a namespace structure.
It is usually not that simple but I know that the least squares function is in the linear algrebra section of the numpy module
numpy doesn't use the numpy.(tool)_package approach... I think esri modified that namespace useage to make it easier for gis peeps.
I prefer to think of arctools in this context arcpy.toolbox.tool with arcpy being a convenience to access tools in the toolbox namespace
Dan Patterson Dan, that makes perfect sense!
So, now I'm wondering why (regardless of the script tool) the tool works one way but not the other in Pro? I don't know if that's a bug or hints at one, or possibly that there is indeed something wrong with the inputs since
arcpy.management.SaveToLayerFile
Returns a 000622 error.
Also, I failed to answer one of Dan's original questions which was if the map project was open. When I run the code from the python window setting the aprx to 'CURRENT' it works fine. However...when I run the code in the python window from another map project and set the aprx to the full file path of the closed project, then I get the same error as in my script tool! So that's something there.