<?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: How to determine in which context a python script is run? in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/how-to-determine-in-which-context-a-python-script/m-p/111202#M3796</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I've wondered this same thing myself. Here's my (imperfect) solution applied to your problem:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;def printMessage(msg):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; allModules = dir()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; if 'Tool Validator' in allModules: # Called from ArcMap&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddMessage(msg)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; else: # Called from command-line&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print msg&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm not sure if this is what you were looking for, but it has worked for me, as I just wanted to change the command-line syntax of arguments. It's a flawed method because 1) There could be a separate module called Tool Validator already loaded into Python that was not loaded by ArcMap and 2) If you don't "run script in process" then the Tool Validator module is not loaded.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope this helps.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 12 May 2010 15:20:00 GMT</pubDate>
    <dc:creator>AustinMilt</dc:creator>
    <dc:date>2010-05-12T15:20:00Z</dc:date>
    <item>
      <title>How to determine in which context a python script is run?</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/how-to-determine-in-which-context-a-python-script/m-p/111196#M3790</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello --&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Python scripts can be used either as stand-alone script or as a script tool. Is there (programmatically speaking) a way to determine in which context a script is run?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Benoit&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 04 May 2010 16:52:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/how-to-determine-in-which-context-a-python-script/m-p/111196#M3790</guid>
      <dc:creator>BenoitThierry</dc:creator>
      <dc:date>2010-05-04T16:52:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to determine in which context a python script is run?</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/how-to-determine-in-which-context-a-python-script/m-p/111197#M3791</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;One method might be to look at the script itself. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The input parameters will be either hard coded or not.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If for instance you inputs look similar to this:&amp;nbsp; Input1 = "c:/temp/shape.shp"&amp;nbsp; or Input2 = "c/project1/point.shp"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;you might conclude that the script can be run as a script. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;However,&amp;nbsp; if the script contains inputs like these: Input1 = sys.argv[1]&amp;nbsp;&amp;nbsp; and&amp;nbsp; Input2 = sys.argv[2]&amp;nbsp; etc..&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;you might conclude that the script is better suited as a script tool.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also maybe check to see if it has been loaded into an ArcToolbox already.&amp;nbsp; If so it has already been converted to a script tool.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 May 2010 18:57:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/how-to-determine-in-which-context-a-python-script/m-p/111197#M3791</guid>
      <dc:creator>JamesHood</dc:creator>
      <dc:date>2010-05-10T18:57:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to determine in which context a python script is run?</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/how-to-determine-in-which-context-a-python-script/m-p/111198#M3792</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Actually, my workflow is: (1) develop the script within Eclipse (with sys.argv), (2) debug it within Eclipse, (3) run it as a tool in ArcGIS or as a script in Windows command line.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As you see, my problem is to determine **at runtime** if the script is called as a tool from within ArcGIS or directly from PythonWin (or others). This would allow me to change the output formating/debug information displayed according to the context (for instance avoiding the use of gp.addmessage when run in command line mode). Your solution would only work to evaluate for which primary purpose the code has been developed (tool or script), right?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In other suggestion?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Benoit&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 May 2010 19:23:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/how-to-determine-in-which-context-a-python-script/m-p/111198#M3792</guid>
      <dc:creator>BenoitThierry</dc:creator>
      <dc:date>2010-05-10T19:23:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to determine in which context a python script is run?</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/how-to-determine-in-which-context-a-python-script/m-p/111199#M3793</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;you can have it both ways by directing your messages to a list, then printing them when desired.&amp;nbsp; for example, you can use:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;messages = []&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;messages.append("hello")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;.....&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;messages.append("another message")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;.....&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;etc&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;then use&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;messages.append("finished processing")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#print messages&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;printMessages(messages)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;and it won't matter whether you are running the script from pythonwin or arctoolbox.&amp;nbsp; A note, if you terminate any process using sys.exit() make sure that you throw the printMessages line before exiting and before the geoprocessor (gp) is deleted&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
def printMessages(messages):
&amp;nbsp; '''provide a list of messages to this method'''
&amp;nbsp; for i in messages:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print i
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddMessage(i)
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:40:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/how-to-determine-in-which-context-a-python-script/m-p/111199#M3793</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-11T06:40:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to determine in which context a python script is run?</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/how-to-determine-in-which-context-a-python-script/m-p/111200#M3794</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;One of the reason why I want to determine script context is to get a clean message output. As you can see from the included JPEG, gp.AddMessage provides a garbled text. Selecting the proper way of displaying a message (print in script context and gp.AddMessage in tool context) would allow me to avoid this problem.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But maybe is there another way of doing this I'm not aware of?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;BenoÃ®t&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 May 2010 15:21:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/how-to-determine-in-which-context-a-python-script/m-p/111200#M3794</guid>
      <dc:creator>BenoitThierry</dc:creator>
      <dc:date>2010-05-11T15:21:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to determine in which context a python script is run?</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/how-to-determine-in-which-context-a-python-script/m-p/111201#M3795</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You might try importing sys and testing sys.executable, in process that is ArcMap.exe (or Catalog).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 May 2010 15:11:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/how-to-determine-in-which-context-a-python-script/m-p/111201#M3795</guid>
      <dc:creator>BruceHarold</dc:creator>
      <dc:date>2010-05-12T15:11:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to determine in which context a python script is run?</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/how-to-determine-in-which-context-a-python-script/m-p/111202#M3796</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I've wondered this same thing myself. Here's my (imperfect) solution applied to your problem:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;def printMessage(msg):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; allModules = dir()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; if 'Tool Validator' in allModules: # Called from ArcMap&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddMessage(msg)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; else: # Called from command-line&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print msg&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm not sure if this is what you were looking for, but it has worked for me, as I just wanted to change the command-line syntax of arguments. It's a flawed method because 1) There could be a separate module called Tool Validator already loaded into Python that was not loaded by ArcMap and 2) If you don't "run script in process" then the Tool Validator module is not loaded.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope this helps.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 May 2010 15:20:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/how-to-determine-in-which-context-a-python-script/m-p/111202#M3796</guid>
      <dc:creator>AustinMilt</dc:creator>
      <dc:date>2010-05-12T15:20:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to determine in which context a python script is run?</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/how-to-determine-in-which-context-a-python-script/m-p/111203#M3797</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Hi&lt;BR /&gt;&lt;BR /&gt;You might try importing sys and testing sys.executable, in process that is ArcMap.exe (or Catalog).&lt;BR /&gt;&lt;BR /&gt;Regards&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Scratch what I said. use Bruce's method (as I am about to do).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 May 2010 15:47:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/how-to-determine-in-which-context-a-python-script/m-p/111203#M3797</guid>
      <dc:creator>AustinMilt</dc:creator>
      <dc:date>2010-05-12T15:47:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to determine in which context a python script is run?</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/how-to-determine-in-which-context-a-python-script/m-p/111204#M3798</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This does it! Thanks!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Benoit&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Hi&lt;BR /&gt;&lt;BR /&gt;You might try importing sys and testing sys.executable, in process that is ArcMap.exe (or Catalog).&lt;BR /&gt;&lt;BR /&gt;Regards&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 May 2010 14:37:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/how-to-determine-in-which-context-a-python-script/m-p/111204#M3798</guid>
      <dc:creator>BenoitThierry</dc:creator>
      <dc:date>2010-05-13T14:37:30Z</dc:date>
    </item>
  </channel>
</rss>

