ArcToolbox scripts behave differently then the hard-coded versions.

2117
4
10-14-2011 11:11 AM
GerryGabrisch
Occasional Contributor III
I made a four color map algorithm for Python. When I execute the script in PythonWin, Komodo, or Idle, with the path to the feature class hard-coded into the script, things run fine. I know the syntax is correct. Once I add the script to ArcToolbox it throws an error (error message posted below). The error occurs at this line.

if (geometry1.touches(geometry2) or geometry1.overlaps(geometry2)) and counter != counter1:

File "E:\SCRIPT~1\MINIMU~1\minimumpolygoncolor.py", line 46, in <module>
if (geometry1.touches(geometry2) or geometry1.overlaps(geometry2)) and counter != counter1:

Error Info:
'unicode' object has no attribute 'touches'
ArcPy ERRORS:


I also developed a tool to remap data paths in an mxd. Again, things run fine if I post the code into the Python window. All the broken links are fixed. When I convert this tool to a Toolbox script, the paths get updated...and then removed from the mxd.

Does anyone know why a script would act differently once they are brought into ArcToolbox?
Tags (2)
0 Kudos
4 Replies
StacyRendall1
Occasional Contributor III
Gerry,

you probably need to post more of your code. The error message you are receiving indicates that the geometry is not actually an object - just a string (some text), in this case a Unicode string, which is how the parameters from Arc are brought in...

You can do some testing with arcpy.AddMessage() just before the line that is failing, for example:
arcpy.AddMessage(str(geometry1)+type(geometry1))

You can then test this in both situations and check it is the same...
0 Kudos
GerryGabrisch
Occasional Contributor III
Here is a portion of the code that returns the error.  Again, it works fine as a stand alone where it is taking the inFC as text just like the arcpy.GetParameterAsText(0)


try:
    print "go"
    import arcpy, os, sys, string, traceback, operator
    inFC = arcpy.GetParameterAsText(0)
    g1 = arcpy.Geometry()
    geometrylist1 = arcpy.CopyFeatures_management(inFC, g1)
    g2 = arcpy.Geometry()
    geometrylist2 = arcpy.CopyFeatures_management(inFC, g2)
    
    for geometry1 in geometrylist1:
        for geometry2 in geometrylist2:
            if (geometry1.touches(geometry2) or geometry1.overlaps(geometry2)):
                print "Do more stuff here..."
    print "done"
except arcpy.ExecuteError:
    msgs = arcpy.GetMessages(2) 
    arcpy.AddError(msgs) 
    print msgs
except:
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
    msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n"
    arcpy.AddError(pymsg)
    arcpy.AddError(msgs)
    print pymsg + "\n"
    print msgs
0 Kudos
GerryGabrisch
Occasional Contributor III
Stacy, thanks for the tip.

When I run the tool in Komodo I get this for the str(geometry1)  and type(geometry 1)

<geoprocessing describe geometry object object at 0x0ECA3D28>
<class 'arcpy.arcobjects.geometries.Polygon'>
<geoprocessing describe geometry object object at 0x0ECA3D28>

In ArctoolBox I get this:

i
<type 'unicode'>
0 Kudos
StacyRendall1
Occasional Contributor III
Gerry,

try putting AddMessage() comments after every operation. There is a good chance that your input isn't coming through quite as you expect it. I.e. make the top part of your code like so:
try:
    print "go"
    import arcpy, os, sys, string, traceback, operator
    inFC = arcpy.GetParameterAsText(0)
    arcpy.AddMessage(str(inFC)+str(type(inFC)))
    g1 = arcpy.Geometry()
    geometrylist1 = arcpy.CopyFeatures_management(inFC, g1)
    arcpy.AddMessage(str(geometrylist1)+str(type(geometrylist1)))
    g2 = arcpy.Geometry()
    geometrylist2 = arcpy.CopyFeatures_management(inFC, g2)
    arcpy.AddMessage(str(geometrylist2)+str(type(geometrylist2)))

0 Kudos