Python script works perfectly in ArcMap Python window and fails as Script Tool

05-14-2019 09:02 AM
New Contributor

I have a simple script to do the following: 1) convert an Excel file to a dBase file, 2) make an event layer from the dBase file, 3) convert the event layer to a feature class, and finally 4) apply symbology from a symbology layer. When I run this script in the Python window in ArcMap it works flawlessly every time (the result is that both the event layer and the new feature class are added to the Table of Contents). When I run it as a script tool it does successfully complete tasks 1-3, but it does not add the event layer or the feature class to the table of contents, and it throws the following error for applying symbology: 

Traceback (most recent call last):
File "C:\Users\Karen\Desktop\", line 13, in <module>
arcpy.ApplySymbologyFromLayer_management("trees", "trees_symbology.lyr")
File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\", line 6951, in ApplySymbologyFromLayer
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Input Layer: Dataset trees does not exist or is not supported
Failed to execute (ApplySymbologyFromLayer).

Does anyone know why it will work in the Python Script window but not as a script tool? Below is my script: 

import arcpy

arcpy.env.workspace = "C:\Users\Karen\Desktop\Using Urban ArcGis\Assignment 3"

arcpy.env.overwriteOutput = True

arcpy.ExcelToTable_conversion("trees.xls", "trees.gdb", "trees")

arcpy.MakeXYEventLayer_management("trees.dbf", "POINT_X", "POINT_Y", "trees_points")

arcpy.FeatureClassToFeatureClass_conversion("trees_points", "Week5.gdb", "trees")

arcpy.ApplySymbologyFromLayer_management("trees", "trees_symbology.lyr")

0 Kudos
3 Replies
MVP Esteemed Contributor

Sharing with Python‌ since this is an ArcPy question and not ArcGIS API for Python question.

0 Kudos
New Contributor

Thanks Joshua!

0 Kudos
MVP Emeritus

Karen... If you examine your path, it needs to be 'raw' encoded.  In python 3, it fails miserably rather than quietly failing with a useless message

pth = "C:\Users\Karen\Desktop\Using Urban ArcGis\Assignment 3"
  File "<ipython-input-1-59b8c5700c1b>", line 1
    pth = "C:\Users\Karen\Desktop\Using Urban ArcGis\Assignment 3"
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

But don't use spaces in folders, it will just cause no end of grief in other situations.

So to see if you can get past the error message, put the little 'r' in front of the path

pth = r"C:\Users\Karen\Desktop\Using Urban ArcGis\Assignment 3"

C:\Users\Karen\Desktop\Using Urban ArcGis\Assignment 3
0 Kudos