Select to view content in your preferred language

NameError: name ' ' is not defined.

7837
5
08-29-2023 11:42 AM
ScoutStanley
Occasional Contributor

Hi all, 

Working on converting a stand alone script from python 2 to 3. When I run it, I get an error "NameError: name 'numRecords' is not defined" . I am having issue trying to get numRecords defined. Any idea on what is causing the issue? Ran fine in python 2. 

Below is section of script related to error. 

 #Find both Frequency tables in the aprx and set them as variables
        FreqTable = m.listTables("Freq")
        Freq1Table = m.listTables("Freq1")
        

        #Add definition query to remove records in freq tables with blank values.
        for lyr in m.listTables(aprx):
            if lyr.name == "Freq":
                lyr.definitionQuery = "AGUSETYPE <> '' AND MUPOLYGON_MUSYM <> ''"	

        for lyr in m.listTables(aprx):
            if lyr.name == "Freq1":
                lyr.definitionQuery = "AGUSETYPE <> ''"


        #Reference page layout elements
        lyt = aprx.listLayouts()[0]
        
        if not lyt.mapSeries is None:
            ms = lyt.mapSeries
            lyt.mapSeries.refresh()
            if ms.enabled:
                ms.currentPageNumber = ms.getPageNumberFromName(PIN)

        #Build selection set
        for t in FreqTable:
            numRecords = int(arcpy.management.GetCount(t).getOutput(0))
            print(str(numRecords))

Below is "Freq" defined, which is within script near the beginning. 

        #Create Frequency table using (AGUSETYPE, MUPOLYGON_MUSYM and Sum byACRES). Then Add Frequency table to aprx.
        arcpy.analysis.Frequency(r'memory\aguse_soil',r'memory\Freq',"AGUSETYPE;MUPOLYGON_MUSYM","ACRES")

        Freq = arcpy.management.MakeTableView(r'memory\Freq')
Tags (2)
0 Kudos
5 Replies
Brian_Wilson
Honored Contributor

This feels like a fragment of a larger script, if line  27 runs then numRecords will be defined and the only other place it's referenced is line 28. If line 27 ran then line 28 should run. So there is no place where the error will trip you up?? Does the error message include a line number? (I test in Visual Studio Code, always.)

For example if I extract these lines they run fine even though I have no FreqTable until the (unindented) print throws an error and it shows be the problem is on line 4

FreqTable = m.listTables("Freq")
for t in FreqTable:
    numRecords = int(arcpy.management.GetCount(t).getOutput(0))
print(numRecords)

NameError                                 Traceback (most recent call last)
      2 for t in FreqTable:
      3     numRecords = int(arcpy.management.GetCount(t).getOutput(0))
----> 4 print(numRecords)

NameError: name 'numRecords' is not defined

 

0 Kudos
ScoutStanley
Occasional Contributor

Yes, this is fragment of a larger script, but I have included all information related to numRecords. The error message includes line number 27, which is below. The error is "numRecords" not defined, which is confusing me because in the script I have it clearly defined, so I am trying to figure out where I am doing something wrong. 

numRecords = int(arcpy.management.GetCount(t).getOutput(0))

 

0 Kudos
Brian_Wilson
Honored Contributor

Have you tried

numRecords = arcpy.management.GetCount(t) # don't need getOutput(0) or int()

print(numRecords) # dont need str()

 

 

0 Kudos
ScoutStanley
Occasional Contributor

I tested that recommendation. It crashes a couple lines down in the script where I 1st reference it with NameError: name 'numRecords' is not defined... However, when it runs it does not complete result of my "print (numRecords) statement that I want included. Below is result error. 

ScoutStanley_0-1694015418081.png

 

0 Kudos
Brian_Wilson
Honored Contributor

I'd love to just load the entire script into my debugger here and run it, can you post the whole thing? Obviously redact private information if you need to.

0 Kudos