<?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 Syntax for import statement in codeblock for python script in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/syntax-for-import-statement-in-codeblock-for/m-p/129272#M10043</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm trying to use the CalculateField tool in a script tool and part of this includes using the arcgis.rand function.&amp;nbsp; I read somewhere that the calculate field tool has it's own globals and that I would need to use the import statement at the begining of the codeblock in order for the tool to work.&amp;nbsp; If I don't import it in the codeblock, I receive an error "Name 'arcgis' is not defined". So, can anyone help in setting the syntax for the import statement.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000004s000000" rel="nofollow noopener noreferrer" target="_blank"&gt;This is an example from the ESRI Resource site ...&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;Expression:
MetersToFeet((float(!shape.area!)))

Expression Type:
PYTHON_9.3

Code Block:
import math
def MetersToFeet(area):
&amp;nbsp; return math.pow(3.2808, 2) * area
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is what I have coded ...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; expression = "id_gen()"

&amp;nbsp;&amp;nbsp;&amp;nbsp; codeblock = """def id_gen(size = 9, chars = string.ascii_uppercase + string.digits):
&amp;nbsp;&amp;nbsp;&amp;nbsp; return ''.join(random.choice(chars) for x in range (size))"""

&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(sewer, fieldName, expression, "PYTHON", codeblock)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 07:17:48 GMT</pubDate>
    <dc:creator>danielchaboya</dc:creator>
    <dc:date>2021-12-11T07:17:48Z</dc:date>
    <item>
      <title>Syntax for import statement in codeblock for python script</title>
      <link>https://community.esri.com/t5/python-questions/syntax-for-import-statement-in-codeblock-for/m-p/129272#M10043</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm trying to use the CalculateField tool in a script tool and part of this includes using the arcgis.rand function.&amp;nbsp; I read somewhere that the calculate field tool has it's own globals and that I would need to use the import statement at the begining of the codeblock in order for the tool to work.&amp;nbsp; If I don't import it in the codeblock, I receive an error "Name 'arcgis' is not defined". So, can anyone help in setting the syntax for the import statement.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000004s000000" rel="nofollow noopener noreferrer" target="_blank"&gt;This is an example from the ESRI Resource site ...&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;Expression:
MetersToFeet((float(!shape.area!)))

Expression Type:
PYTHON_9.3

Code Block:
import math
def MetersToFeet(area):
&amp;nbsp; return math.pow(3.2808, 2) * area
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is what I have coded ...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; expression = "id_gen()"

&amp;nbsp;&amp;nbsp;&amp;nbsp; codeblock = """def id_gen(size = 9, chars = string.ascii_uppercase + string.digits):
&amp;nbsp;&amp;nbsp;&amp;nbsp; return ''.join(random.choice(chars) for x in range (size))"""

&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(sewer, fieldName, expression, "PYTHON", codeblock)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 07:17:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/syntax-for-import-statement-in-codeblock-for/m-p/129272#M10043</guid>
      <dc:creator>danielchaboya</dc:creator>
      <dc:date>2021-12-11T07:17:48Z</dc:date>
    </item>
    <item>
      <title>Re: Syntax for import statement in codeblock for python script</title>
      <link>https://community.esri.com/t5/python-questions/syntax-for-import-statement-in-codeblock-for/m-p/129273#M10044</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Daniel,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I think your better off using arcpy.da.UpdateCursor(). So you can work directly with the data rather than call the CalculateFieldTool first from the script to do your field calculations. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanos&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I'm trying to use the CalculateField tool in a script tool and part of this includes using the arcgis.rand function.&amp;nbsp; I read somewhere that the calculate field tool has it's own globals and that I would need to use the import statement at the begining of the codeblock in order for the tool to work.&amp;nbsp; If I don't import it in the codeblock, I receive an error "Name 'arcgis' is not defined". So, can anyone help in setting the syntax for the import statement.&amp;nbsp; &lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000004s000000" rel="nofollow noopener noreferrer" target="_blank"&gt;This is an example from the ESRI Resource site ...&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;Expression:
MetersToFeet((float(!shape.area!)))

Expression Type:
PYTHON_9.3

Code Block:
import math
def MetersToFeet(area):
&amp;nbsp; return math.pow(3.2808, 2) * area
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;This is what I have coded ...&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; expression = "id_gen()"

&amp;nbsp;&amp;nbsp;&amp;nbsp; codeblock = """def id_gen(size = 9, chars = string.ascii_uppercase + string.digits):
&amp;nbsp;&amp;nbsp;&amp;nbsp; return ''.join(random.choice(chars) for x in range (size))"""

&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(sewer, fieldName, expression, "PYTHON", codeblock)&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 07:17:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/syntax-for-import-statement-in-codeblock-for/m-p/129273#M10044</guid>
      <dc:creator>ChrisPedrezuela</dc:creator>
      <dc:date>2021-12-11T07:17:51Z</dc:date>
    </item>
    <item>
      <title>Re: Syntax for import statement in codeblock for python script</title>
      <link>https://community.esri.com/t5/python-questions/syntax-for-import-statement-in-codeblock-for/m-p/129274#M10045</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Just need to import random&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;codeblock = """import random
def id_gen(size = 9, chars = string.ascii_uppercase + string.digits):
&amp;nbsp;&amp;nbsp;&amp;nbsp; return ''.join(random.choice(chars) for x in range (size))"""&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 07:17:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/syntax-for-import-statement-in-codeblock-for/m-p/129274#M10045</guid>
      <dc:creator>JasonScheirer</dc:creator>
      <dc:date>2021-12-11T07:17:54Z</dc:date>
    </item>
  </channel>
</rss>

