3

I created a script that takes a .tab file and joins it to a shapefile. The script works perfectly but when it finishes, it gives me an unhandled exception which crashes Python.

I don't know what might be causing it but I think I have identify where it's happening. I wanted to ask if someone could help me identify the problem so I can fix it. I'm using ArcPy 10.1 and Python 2.7. I think it's happening in this part. For all the variables I get values prior to this portion of the script. I do the same calculation for other fields so this code repeats itself but for different columns.

#Create and calculate new fields
        #Population
        if(p == 1):
            pop_calc = '!pop_20{}!-!pop_2010!'.format(year)
            arcpy.AddField_management(outFeature, "pop_diff","LONG",15,"","","","")
            arcpy.CalculateField_management(outFeature,'pop_diff',pop_calc,"PYTHON_9.3")
            arcpy.AddField_management(outFeature, "popShare","DOUBLE",15,10,"","","")
            popSharedTotal = 0            
            popFields = 'pop_diff'
            with arcpy.da.SearchCursor(fc, popFields)as cursor:
                for row in cursor:
                    popSharedTotal = row[0] + popSharedTotal                    
            arcpy.AddField_management(outFeature, 'temp',"LONG",15,"","","","")
            arcpy.CalculateField_management(outFeature,'temp',popSharedTotal,"PYTHON_9.3")
            arcpy.CalculateField_management(outFeature,'popShare','[pop_diff]/[temp]',"VB")
            arcpy.DeleteField_management(outFeature,'temp')

I can provide the entire script if needed but by commenting sections, this portion is what makes Python not close correctly when done with the script.

enter image description here

  • 1
    Can you show the error message? – dklassen Feb 11 '14 at 22:19
  • 1
    It does not give me any error just when the script is done it gives me this. So i dont know where is happening – Daniel Jimenez Feb 11 '14 at 22:29
  • 1
    What are you using to run your python script? Pythonwin? command line? – dklassen Feb 11 '14 at 22:42
  • There is advice offered here that may or may not apply to your situation. In any event, I think you need to try and get a standalone code snippet, that can be run independently to still trigger the error, extracted from your application. – PolyGeo Feb 11 '14 at 23:12
  • I'm using both, python win and just clicking the script. When i run it with Pythonwin the error shows up but disappears very quick. When i run the program by clicking the script i get the error in the picture. – Daniel Jimenez Feb 11 '14 at 23:32
2

This is an simply an arcpy bug. You can try to avoid using the steps that are causing the crash, but it generally happens under different tools when used to process through a long list of data. The only workaround I have found is to make my script save its progress along the way to disk so if you restart the process, it knows where to pickup from. If you then disable windows debugger message by altering the registry (see below), you can then just repeatedly execute the script in cmd.exe until it completes the entire batch.

I know this is an awful workaround, but it is quite uncommon to have a python library kill off the python interpreter.

DWORD HKLM or HKCU\Software\Microsoft\Windows\Windows Error Reporting\DontShowUI = "1"
DWORD HKLM or HKCU\Software\Microsoft\Windows\Windows Error Reporting\Disabled = "1"
2

Maybe a little late to this, but I'm hoping others can benefit from my solution also. I ran into the same issue, where I got the python.exe has stopped working during the arcpy.CalculateField_management function. Lucas Fortini mentioned here that it might be due to having large lists of data, i.e. too many features. I have >1 million polygon features, and growing. I fixed it by using the arcpy.da.UpdateCursor instead of arcpy.CalculateField_management. Here's a snippet of my code:

Instead of:

arcpy.CalculateField_management(feature_layer, field, val, "PYTHON_9.3")

I used:

fields = [field]
with arcpy.da.UpdateCursor(feature_layer, fields) as cursor:
    for row in cursor:
        row[0] = val
        cursor.updateRow(row) 
1

I found what was causing the problem. it was the calculation using a python expression in the arcpy.CalculateField_management() method. I changed the expression type to "VB" and python is not crashing any more. The reason of why it crashes using a python expression is beyond me. The modified code looks like this:

#Cerate and calculate new fields
    #Population
    if(p == 1):
        pop_calc = '[pop_20{}]-[pop_2010]'.format(year)
        arcpy.AddField_management(outFeature, "pop_diff","LONG",15,"","","","")
        arcpy.CalculateField_management(outFeature,'pop_diff',pop_calc,"VB")
        arcpy.AddField_management(outFeature, "popShare","DOUBLE",15,10,"","","")
        popSharedTotal = 0            
        popField = 'pop_diff'
        with arcpy.da.SearchCursor(fc, popFields)as cursor:
            for row in cursor:
                popSharedTotal = row[0] + popSharedTotal                    
        arcpy.AddField_management(outFeature, 'temp',"LONG",15,"","","","")
        arcpy.CalculateField_management(outFeature,'temp',popSharedTotal,"VB")
        arcpy.CalculateField_management(outFeature,'popShare','[pop_diff]/[temp]',"VB")
        arcpy.DeleteField_management(outFeature,'temp')
0

Changing the expression type to VB may not always solve the problem, as VB won't allow for spatial calculations. One solution may be to use the 64 bit version, allowing for improved processing. This creates other potential problems with running (most likely) two separate Python versions but it's the only work around I've found. A few of the potential confusions are explained in this ESRI blog post. https://blogs.esri.com/esri/supportcenter/2013/07/29/64-bit-vs-32-bit-python-explained/.

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.