Select to view content in your preferred language

Cannot use third-party IDE to debug cursors

875
3
09-26-2020 12:29 PM
MKF62
by
Frequent Contributor

I am trying to debug my scripts which include multiple search/update cursors in them. Any time I set a breakpoint, the cursors will not populate. If I set no breakpoints, the script runs exactly as I want it too, so it does not appear to be a coding problem.

I am working with Pro 2.6.1. I have tried two different IDEs, Visual Studio Enterprise 2019 version 16.7.4 and PyCharm 2019.3. I have also tried using ArcMap Python 2.7 interpreter and a regular file geodatabase instead of an SDE db to no avail.

These are the two different ways in which I have tried to set my breakpoints and step through the code. First, I tried to set the breakpoint before initiating the cursor and then I just step over lines until the for loop is hit. It then skips directly to the "del" statement and does not enter the for loop, even though the cursor should be selecting many features:

if int(arcpy.GetCount_management(coveyCountFL).getOutput(0)) >= 1: ### BREAKPOINT HERE ###
    with arcpy.da.UpdateCursor(coveyCountFL, ['GlobalID', 'EditingPhase', 'created_date']) as uCursor:
        for coveyCountRow in uCursor:
            coveyCountRow[1] = 'Not available for editing.'
            try:
                uCursor.updateRow(coveyCountRow)
            except:
                print(traceback.format_exc())
        del uCursor‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

If I place the breakpoint inside the for loop, it does actually stop (like it should), but then it errors when trying to update the row.

if int(arcpy.GetCount_management(coveyCountFL).getOutput(0)) >= 1:
    with arcpy.da.UpdateCursor(coveyCountFL, ['GlobalID', 'EditingPhase', 'created_date']) as uCursor:
        for coveyCountRow in uCursor:
            coveyCountRow[1] = 'Not available for editing.' ### BREAKPOINT HERE ###
            try:
                uCursor.updateRow(coveyCountRow) ### THROWS EXCEPTION HERE ###
            except:
                print(traceback.format_exc())
        del uCursor‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Traceback (most recent call last):
File "E:\NET_Projects\CollectorGPServices\FallCoveyCount\test.py", line 63, in <module>
uCursor.updateRow(coveyCountRow)
StopIteration: iteration not started

As I step through the code, I have checked 

list(uCursor)

in the watch window to see what it returns. Sometimes it remains an empty list [], other times it returns things, but then if you add "coveyCountRow" in the watch window, list(uCursor) turns to [] so it seems the cursor is always empty even if it says it is not for a second.

I really need to be able to debug my code outside of Pro... has someone figured this issue out yet??

Tags (2)
0 Kudos
3 Replies
DanPatterson
MVP Esteemed Contributor
good = int(arcpy.GetCount_management(coveyCountFL).getOutput(0))
if good >= 1:
    .....

you ruled out your first condition... not sure if a breakpoint within a condition is a good idea.


... sort of retired...
0 Kudos
MKF62
by
Frequent Contributor

Not sure what you mean by I ruled it out? I don’t see a difference between your code and my code other than being in one line instead of two. The Get Count will resolve before it tests the conditional if that’s what you’re concerned about. You can definitely have breakpoints inside of conditionals or on an "if" line. Even if I put the breakpoint one line before the "if" statement, I get the same results. 

Regardless, in my actual script I don’t have that conditional there, I just put that in to show that list(uCursor) should have items in it if it gets past that conditional, which when I step through, it does get inside the conditional when I test my code with one.

I really don't think this is a code problem because otherwise my code wouldn't run if I run it without debugging.

0 Kudos
MKF62
by
Frequent Contributor

Well...my code works again after doing absolutely nothing that makes any logical sense to fix it. I have absolutely no confidence that I won't encounter this problem again, so it'd be nice to still find a real solution to this problem. Anyway, these are the exact steps I took:

1) Set up script to run an update cursor on another feature class (I did not test with a feature layer) from the same SDE database.

2) Put a breakpoint on a line or two before you create the update cursor

3) Run in debug mode, and when you hit the breakpoint, hit continue to run through the rest of the code without stepping through it

4) Check that it updated the field you wanted it to when it's finished running

5) Change the value to update and run in debug mode again, this time stepping through each line. It should work as expected.

6) Run script that was having issues before (which had a feature layer in the update cursor instead of a feature class in this case - I did not change it to a feature class and left it as a feature layer), putting a breakpoint a line or two before you create the cursor and step through. It should work as expected. 

Note that this was specific to each IDE. I had to follow the same steps in each IDE to fix it for each one. Fixing one IDE does not mean the code will run in the other one. 

0 Kudos