Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I've a geoprocessing script that I'm trying to get working. I've turned on Show Messages, so I can see how far in the script I've gotten before the submitted job fails, but it's not helping, as I'm getting errors that I don't understand exactly, because I don't know where they occurred. Is there any way to debug server geoprocessing scripts on the server?

EDIT: Error is occurring in this code, which worked fine locally. I'm not getting either of the messages returned, so something seems to be going wrong with the layers.

for layer in mxdLayers:
        arcpy.AddMessage("Layer name is %s" % (layer.name))
        layer.visible = visibleLayers[layer.name]
        arcpy.AddMessage("Layer not set: " + layer.name)

Error message is Layer: Unexpected error

Edit 2: The error is not in the code above. The following code takes the parameter and stores the passed in features in a scratch gdb. Currently this is passed as an empty string, as I haven't gotten to testing this part yet.

# Get the passed point graphics and save to scratch workspace
inPointFset = arcpy.GetParameterAsText(9)
if inPointFset == '#' or not inPointFset:
    inPointFset = None
    arcpy.CopyFeatures_management(inPointFset, os.path.join(arcpy.env.scratchWorkspace, r"scratch.gdb\Points"))

This code then changes the data source of the first mxd layer (which is why it's blowing up; the first layer doesn't reference the points feature class in the scratch gdb), but since the parameter was an empty string, I'm not sure why this is evaluating to true.

mxdLayers = arcpy.mapping.ListLayers(mapDoc)
if inPointFset:
    mxdLayers[0].replaceDataSource(os.path.join(arcpy.env.scratchWorkspace, r"scratch.gdb"), "FILEGDB_WORKSPACE", "Points")
share|improve this question
Is there any way you can run the script locally -- be it on your machine or the server machine itself? That is to say, run it as an unpublished service/script. Doing so should allow for much more informative output for debugging. –  AHigh Oct 25 '12 at 15:44
I did, though I had to hard code the parameters, and it worked. That's why I'm struggling with debugging on the server, as I can't comprehend what is going on. It's failing at a point past where the parameters are processed, and since that's the only difference other than paths (which aren't failing either) I'm lost. –  Shawn Oct 25 '12 at 15:50
Can you provide the error messages here? If you could also provide at least the portion of the script that is causing the trouble it would be easier to diagnose. –  AHigh Oct 25 '12 at 16:02
10 or 10.1? (I suspect 10 as you say "show messages"). How have you referenced the MXD? I'm guessing a full path and the MXD code works? (you know that "CURRENT" doesnt work in a GP service...) Could you post a larger code snippet showing whats happening before? –  KHibma Oct 25 '12 at 16:34
10. Mxd is referenced via absolute path, and I get the mxd just fine. I've actually tracked down the problem, which is that an optional parameter is empty, but when I evaluate the variable that's supposed to store the features for that optional parameter, it's behaving as if it wasn't empty when it was passed in. In other words, I'm figuring out what's going on; it's just taking forever since I can't debug in place. I've updated the question again to show the actual problem, but it seems as if the answer to my question is No, you can't debug a service on the server. –  Shawn Oct 25 '12 at 18:25
show 2 more comments

1 Answer

I typically use a simple error handling class:

import sys
import traceback
import inspect

class ErrorHandling(object):
    ErrorHandling Provides User/Developers with detailed information about
    errors that occur in the code.  By using this class, you can better
    debug a program or solve on site issues.

    def __init__(self):
        Constructor - no parameters

    def trace(self):
        trace finds the line, the filename and error message and returns it 
        to the user
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
        # script name + line number
        line = tbinfo.split(", ")[1]
        filename = inspect.getfile( inspect.currentframe() )
        # Get Python syntax error
        synerror = traceback.format_exc().splitlines()[-1]
        return line, filename, synerror

Then I reference it in a try except clause.

import arcpy, os 
import ErrorHandling as ErrorHandling
            <your code>
except arcpy.ExecuteError:
            EH = ErrorHandling.ErrorHandling()
            line, filename, err = EH.trace()
            m = "Python error on " + line + " of " + __file__ + \
                " : with error - " + err

This way if any of my code errors it will give me a line number and explanation of the error.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.