<?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 Re: Why does this toolbox script fail the second time? NameError: global name 'arcpy' is not defined in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/why-does-this-toolbox-script-fail-the-second-time/m-p/345848#M27128</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Don't know the differences between the server version and *map or *pro. That leave python 2.7 or server or moving emit out of the class and just calling it&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 21 May 2019 17:04:32 GMT</pubDate>
    <dc:creator>DanPatterson_Retired</dc:creator>
    <dc:date>2019-05-21T17:04:32Z</dc:date>
    <item>
      <title>Why does this toolbox script fail the second time? NameError: global name 'arcpy' is not defined</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-toolbox-script-fail-the-second-time/m-p/345845#M27125</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi All,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Really simple script attached to a toolbox tool:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;has a wrapper class to convert logging to arcpy.AddMessage.&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;no input or output parameters&lt;/LI&gt;&lt;LI&gt;has a main function that adds a single message to the window&lt;/LI&gt;&lt;LI&gt;imports arcpy and logging only&lt;/LI&gt;&lt;LI&gt;runs successfully the first time&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;fails every time after that until the arcmap/arccatalog app is restarted&lt;/LI&gt;&lt;LI&gt;ArcGIS Desktop 10.6.1&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Zip file attached with script and toolbox. Here's the script:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; arcpy 
&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; logging

&lt;SPAN class="keyword token"&gt;class&lt;/SPAN&gt; &lt;SPAN class="token class-name"&gt;AddMessageHandler&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;logging&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;Handler&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    &lt;SPAN class="keyword token"&gt;def&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;emit&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;self&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; record&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
        log_entry &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; self&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;record&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
        arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;AddMessage&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;log_entry&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;

&lt;SPAN class="keyword token"&gt;def&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;main&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    logging&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;debug&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"----this is a test----"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;


&lt;SPAN class="keyword token"&gt;if&lt;/SPAN&gt; __name__ &lt;SPAN class="operator token"&gt;==&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'__main__'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    logging&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;basicConfig&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;level&lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;logging&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;DEBUG&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    
    root_logger &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; logging&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;getLogger&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    root_logger&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;addHandler&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;AddMessageHandler&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;

    main&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;First run success:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/448045_pastedImage_1.png" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Subsequent runs fail:&lt;/P&gt;&lt;P&gt;&lt;IMG class="image-2 jive-image" src="https://community.esri.com/legacyfs/online/448052_pastedImage_2.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 16:17:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-toolbox-script-fail-the-second-time/m-p/345845#M27125</guid>
      <dc:creator>FredSpataro</dc:creator>
      <dc:date>2021-12-11T16:17:19Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this toolbox script fail the second time? NameError: global name 'arcpy' is not defined</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-toolbox-script-fail-the-second-time/m-p/345846#M27126</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Works in Python 3.6.8 within ArcGIS Pro.&lt;/P&gt;&lt;P&gt;Are you using python 2.7?&lt;/P&gt;&lt;P&gt;In any event, give this a shot, which also worked flawlessly&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; arcpy 
&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; logging

&lt;SPAN class="keyword token"&gt;class&lt;/SPAN&gt; &lt;SPAN class="token class-name"&gt;AddMessageHandler&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;logging&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;Handler&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    &lt;SPAN class="keyword token"&gt;def&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;emit&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;self&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; record&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
        log_entry &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; self&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;record&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
        arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;AddMessage&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;log_entry&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;

&lt;SPAN class="keyword token"&gt;def&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;main&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    logging&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;basicConfig&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;level&lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;logging&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;DEBUG&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    root_logger &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; logging&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;getLogger&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    root_logger&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;addHandler&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;AddMessageHandler&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    logging&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;debug&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"----this is a test----"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;


&lt;SPAN class="keyword token"&gt;if&lt;/SPAN&gt; __name__ &lt;SPAN class="operator token"&gt;==&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'__main__'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    main&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 16:17:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-toolbox-script-fail-the-second-time/m-p/345846#M27126</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-11T16:17:22Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this toolbox script fail the second time? NameError: global name 'arcpy' is not defined</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-toolbox-script-fail-the-second-time/m-p/345847#M27127</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes it's python 2.7, ArcGIS 10.6.1... ArcGIS Server does not support python 3.x, Pro is not an option.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Moving the logging lines does not change the result.&amp;nbsp; Runs fine the first time, fails the every time after that.&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 May 2019 15:00:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-toolbox-script-fail-the-second-time/m-p/345847#M27127</guid>
      <dc:creator>FredSpataro</dc:creator>
      <dc:date>2019-05-21T15:00:12Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this toolbox script fail the second time? NameError: global name 'arcpy' is not defined</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-toolbox-script-fail-the-second-time/m-p/345848#M27128</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Don't know the differences between the server version and *map or *pro. That leave python 2.7 or server or moving emit out of the class and just calling it&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 May 2019 17:04:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-toolbox-script-fail-the-second-time/m-p/345848#M27128</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2019-05-21T17:04:32Z</dc:date>
    </item>
  </channel>
</rss>

