<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic NameError name is not defined - stand alone script in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/nameerror-name-is-not-defined-stand-alone-script/m-p/1195659#M7591</link>
    <description>&lt;P&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;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.&amp;nbsp; When I move rec under “Global rec” and the script runs in IDLE, but populates the entire column with 1.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Here is the python code I'm trying to use. Any help would be greatly appreciated!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;sortFeat = Road_Activity_Reports&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;sortField = 'open_datetime'&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;idField = 'open_rep_cnt'&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;rec = 0&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;def autoIncrement():&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;global rec&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;pStart = 1&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;pInterval = 1&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;if (rec == 0):&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;rec = pStart&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;else:&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;rec += pInterval&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;return rec&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;rows = arcpy.UpdateCursor(sortFeat, "", "", "", sortField + " A")&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;for row in rows:&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;row.setValue(idField, autoIncrement())&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;rows.updateRow(row)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;del row, rows&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 25 Jul 2022 21:48:04 GMT</pubDate>
    <dc:creator>Ryan_Gould</dc:creator>
    <dc:date>2022-07-25T21:48:04Z</dc:date>
    <item>
      <title>NameError name is not defined - stand alone script</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/nameerror-name-is-not-defined-stand-alone-script/m-p/1195659#M7591</link>
      <description>&lt;P&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;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.&amp;nbsp; When I move rec under “Global rec” and the script runs in IDLE, but populates the entire column with 1.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Here is the python code I'm trying to use. Any help would be greatly appreciated!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;sortFeat = Road_Activity_Reports&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;sortField = 'open_datetime'&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;idField = 'open_rep_cnt'&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;rec = 0&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;def autoIncrement():&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;global rec&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;pStart = 1&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;pInterval = 1&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;if (rec == 0):&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;rec = pStart&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;else:&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;rec += pInterval&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;return rec&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;rows = arcpy.UpdateCursor(sortFeat, "", "", "", sortField + " A")&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;for row in rows:&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;row.setValue(idField, autoIncrement())&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;rows.updateRow(row)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;del row, rows&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jul 2022 21:48:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/nameerror-name-is-not-defined-stand-alone-script/m-p/1195659#M7591</guid>
      <dc:creator>Ryan_Gould</dc:creator>
      <dc:date>2022-07-25T21:48:04Z</dc:date>
    </item>
    <item>
      <title>Re: NameError name is not defined - stand alone script</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/nameerror-name-is-not-defined-stand-alone-script/m-p/1195668#M7592</link>
      <description>&lt;P&gt;&amp;nbsp;I figured it out.&lt;/P&gt;&lt;P&gt;"rec" needs to be defined outside the Main(). I add this under "import arcpy" and the script runs fine.&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;import arcpy&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;rec = 0&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jul 2022 22:35:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/nameerror-name-is-not-defined-stand-alone-script/m-p/1195668#M7592</guid>
      <dc:creator>Ryan_Gould</dc:creator>
      <dc:date>2022-07-25T22:35:36Z</dc:date>
    </item>
  </channel>
</rss>

