3

I have been woking on a script that will select parcels that are within a particular ArcGIS 10.1 data driven page, create a list out of those parcels, and then populate a text element within my MXD with the parcel list.

I run this script from CMD right after I restart the machine, and it works fine. But as soon as I try to rerun it(without reboot), the script errors out. I get no error message at all other than the generic Python.exe has stopped working.

I work on a workstation with Windows 7 Pro, 3.4GHz processor with 8GB RAM, ArcDesktop 10.1, and python 2.7.

What might be the possible reasons for python to fail? Are there issues within my script? Any help would be greatly appreciated.

The print statements are in the script as I was trying to trouble shoot where it was crashing.

import sys
import arcinfo
import arcpy
sys.path.append("R:\ARCGIS_SCRIPTS\General")
import DatabaseFunctions
arcpy.ClearWorkspaceCache_management()

def main(args=None):
    #if args is None:
        #args = sys.argv[1:]
    #UserInput = (args[0])
    UserInput = arcpy.GetParameterAsText(0)


try:
    try:
        arcpy.AddMessage('1')
        arcpy.CheckInExtension("Foundation")
        arcpy.AddMessage('1')
    except Exception as e:
        print e
    try:
        arcpy.AddMessage('1')
        arcpy.CheckInExtension("Datareviewer")
        arcpy.AddMessage('1')
    except Exception as e:
        print e

    ## Input from user
    PageNameList = [UserInput] #["1N1E03"]
    #OutputFolder = "C:\\Dan\\PythonWrestling"
    OutputFolder = "C:\\Joey"

    ## Hard coded resources
    MapFolder = "C:\\Joey"
    #MapFolder = "C:\\Dan\\PythonWrestling"
    vector = DatabaseFunctions.CreateDatabaseConnection(Database="Vector")
    mxd = arcpy.mapping.MapDocument(MapFolder + "\\SectionMap.mxd")
    arcpy.AddMessage(mxd)
    layerlist = None
    del layerlist
    layerlist = arcpy.mapping.ListLayers(mxd)
    arcpy.AddMessage(layerlist)
    for lyr in layerlist:
        if lyr.name == "Parcels":
            PLyr = lyr
        if lyr.name == "Page":
            PGLyr = lyr
    #PLyr = arcpy.mapping.ListLayers(mxd, "Parcels")[0]
    #arcpy.AddMessage('1')
    #PGLyr = arcpy.mapping.ListLayers(mxd, "Page")[0]

    ddp = mxd.dataDrivenPages
    for pageName in PageNameList:
        ddp.currentPageID = ddp.getPageIDFromName(pageName)

        ## Create parcel list
        arcpy.MakeFeatureLayer_management(vector + "\\vector.DBO.parcels", "thingy")
        arcpy.SelectLayerByLocation_management ("thingy", "HAVE_THEIR_CENTER_IN" , PGLyr)
        sCursor = arcpy.SearchCursor("thingy",fields="PARCEL")
        parcelSetUnsorted = set()
        for row in sCursor:
            eachParcel = row.PARCEL
            if isinstance(eachParcel,basestring):
                parcelSetUnsorted.add(eachParcel)
        parcelSet = sorted(parcelSetUnsorted)
        parcelListStringList = list()
        parcelListText = ""
        count = 0
        print str(len(parcelSet)) + " parcels in the set."
        for eachParcel in parcelSet:
            count = count + 1
            parcelListText = parcelListText + eachParcel + "\n"
            if count == 70:
                if (len(parcelListStringList) == 5):
                    parcelListText = parcelListText + "And more..."
                    parcelListStringList.append(parcelListText[:])
                    parcelListText = ""
                    continue
                parcelListStringList.append(parcelListText[:])
                count = 0
                parcelListText = ""
        if len(parcelListText) > 0:
            parcelListStringList.append(parcelListText[:])

        arcpy.AddMessage(parcelListStringList)

        ## Put correct text in each text element
        elmList = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT")
        upperx = .5
        uppery = 10.0
        ydelta = .75
        xdelta = .9
        ylower = .75

        for elm in elmList:
            if elm.name == "ParcelList":
                elementCount = 0
                elm.elementPositionX = -5
                for parList in parcelListStringList:
                    eachElm = elm.clone("_clone" + str(elementCount))
                    eachElm.elementPositionX = upperx + (elementCount * xdelta)
                    elementCount = elementCount + 1
                    print "--- " + parList
                    eachElm.text = parList
            #elif elm.name == "MapTitle":
                # elm.text = titleText
        ## Export as a PDF
        arcpy.AddMessage('Exporting Page')
        mxd.saveACopy(OutputFolder + "\\" + str(pageName) + ".mxd")
        #arcpy.mapping.ExportToPDF(mxd, OutputFolder + "\\" + str(pageName) + ".pdf")
except Exception as e:
    print e
del mxd



if __name__ == "__main__":
    sys.exit(main())

migrated from stackoverflow.com May 8 '14 at 18:32

This question came from our site for professional and enthusiast programmers.

  • 1
    How is it failing? What does "errors out" mean? What errors are you getting? Those messages are critical to understanding the problem, you should never omit them when asking for help. – Jonathon Reinhart May 7 '14 at 23:01
  • That is the thing. "Errors out" might have been the wrong term, the program just quits. I am getting no error messages. I just get the generic windows pop up saying that python.exe has stopped working. – user3614153 May 8 '14 at 13:50
  • Seems like a bug in the C code then. Either Python itself, or is arcpy a python or C extension? – Jonathon Reinhart May 8 '14 at 13:53
  • arcpy is a python extension that comes with ArcGIS. – user3614153 May 8 '14 at 15:07
  • 1
    Have you tried loading and running the script into the Python window in either Arc Catalog or Map? This might be useful than implementing the script from the CMD. Usually the Python window in Arc will return a more specific error, however the problem you are having sounds more unique that just a general code error. – F_Kellner May 8 '14 at 18:59
1

In cases like this I think you can spend a lot more time trying to troubleshoot the interaction between various aspects of your configuration (ArcGIS for Desktop, Python, Operating System, Environment Variables, etc, etc) than it takes to uninstall/reinstall ArcGIS for Desktop (with Python as part of that install) i.e. about 30 minutes.

If it is critical for you to get Python working predictably with ArcGIS 10.1 for Desktop on this machine then, if preliminary troubleshooting does not provide a solution, this step probably will.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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