NameError name is not defined - stand alone script

4037
1
Jump to solution
07-25-2022 02:48 PM
Ryan_Gould
New Contributor III

This is probably a very simple question: I would like to run a standalone script that populates a field with a sequential ID sorted by a datetime field. I can run the code (below) successfully in the ArcGIS Pro python window, but when I try to run it in IDLE 3.7 the “rec” variable is not recognized.

The error I receive is: “NameError: name 'rec' is not defined”. As I understand it, this means the code doesn’t know what “rec” is.  When I move rec under “Global rec” and the script runs in IDLE, but populates the entire column with 1. 

Here is the python code I'm trying to use. Any help would be greatly appreciated!

sortFeat = Road_Activity_Reports
sortField = 'open_datetime'
idField = 'open_rep_cnt'

rec = 0

def autoIncrement():
global rec

pStart = 1
pInterval = 1
if (rec == 0):
rec = pStart
else:
rec += pInterval
return rec

rows = arcpy.UpdateCursor(sortFeat, "", "", "", sortField + " A")

for row in rows:
row.setValue(idField, autoIncrement())
rows.updateRow(row)

del row, rows

0 Kudos
1 Solution

Accepted Solutions
Ryan_Gould
New Contributor III

 I figured it out.

"rec" needs to be defined outside the Main(). I add this under "import arcpy" and the script runs fine.

import arcpy
rec = 0

View solution in original post

0 Kudos
1 Reply
Ryan_Gould
New Contributor III

 I figured it out.

"rec" needs to be defined outside the Main(). I add this under "import arcpy" and the script runs fine.

import arcpy
rec = 0

0 Kudos