<?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 can I test to determine if script is being run in ArcMap vs ArcCatalog? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-can-i-test-to-determine-if-script-is-being-run/m-p/116829#M9184</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You could use try-except statement put your above code inside it &lt;/P&gt;&lt;P&gt;&lt;A href="http://desktop.arcgis.com/en/desktop/latest/analyze/python/error-handling-with-python.htm" title="http://desktop.arcgis.com/en/desktop/latest/analyze/python/error-handling-with-python.htm"&gt;Error handling with Python—Help | ArcGIS for Desktop&lt;/A&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 01 Dec 2015 02:32:39 GMT</pubDate>
    <dc:creator>WesMiller</dc:creator>
    <dc:date>2015-12-01T02:32:39Z</dc:date>
    <item>
      <title>How can I test to determine if script is being run in ArcMap vs ArcCatalog?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-test-to-determine-if-script-is-being-run/m-p/116828#M9183</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a script that can currently run in ArcMap or ArcCatalog, by design. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, if it is run in in ArcMap, I would like the two final output FC to be added to the map (Saving is optional at this point, so haven't added )&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I was able to get this to work in ArcMap with the following addition code..&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# my variables finalOutdd and finalOutProj are set to full paths of the output
mxd = arcpy.mapping.MapDocument("CURRENT")
df&amp;nbsp; = arcpy.mapping.ListDataFrames(mxd)[0]
ddLayer = arcpy.mapping.Layer(finalOutdd)
projLayer = arcpy.mapping.Layer(finalOutProj)
arcpy.mapping.AddLayer(df, ddLayer, "TOP")
arcpy.mapping.AddLayer(df, projLayer, "TOP")
# mxd.save()&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My question is, how can I do a test to see if I'm running this in ArcMap?&amp;nbsp;&amp;nbsp;&amp;nbsp; ArcCatalog and an external python window will basically work the same, i.e. not add the layers to anything.&amp;nbsp; But if the code above is used, it will error out that there is no MapDocument&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've looked at the options for arcpy.GetInstallInfo()[""]&amp;nbsp; and &lt;CODE&gt;&lt;SPAN class="n"&gt;arcpy&lt;/SPAN&gt;&lt;SPAN class="o"&gt;.&lt;/SPAN&gt;&lt;SPAN class="n"&gt;ProductInfo&lt;/SPAN&gt;&lt;SPAN class="p"&gt;() but so far, it sees both as "Desktop" (ProduceName from GetInstallInfo)&amp;nbsp; and "ArcInfo" (from ProductInfo). &lt;/SPAN&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="p"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="p"&gt;Anyone know a simple way to test for this?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="p"&gt;Thanks.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="p"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="p"&gt;(btw - Desktop 10.3.1)&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:52:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-test-to-determine-if-script-is-being-run/m-p/116828#M9183</guid>
      <dc:creator>RebeccaStrauch__GISP</dc:creator>
      <dc:date>2021-12-11T06:52:13Z</dc:date>
    </item>
    <item>
      <title>Re: How can I test to determine if script is being run in ArcMap vs ArcCatalog?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-test-to-determine-if-script-is-being-run/m-p/116829#M9184</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You could use try-except statement put your above code inside it &lt;/P&gt;&lt;P&gt;&lt;A href="http://desktop.arcgis.com/en/desktop/latest/analyze/python/error-handling-with-python.htm" title="http://desktop.arcgis.com/en/desktop/latest/analyze/python/error-handling-with-python.htm"&gt;Error handling with Python—Help | ArcGIS for Desktop&lt;/A&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 01 Dec 2015 02:32:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-test-to-determine-if-script-is-being-run/m-p/116829#M9184</guid>
      <dc:creator>WesMiller</dc:creator>
      <dc:date>2015-12-01T02:32:39Z</dc:date>
    </item>
    <item>
      <title>Re: How can I test to determine if script is being run in ArcMap vs ArcCatalog?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-test-to-determine-if-script-is-being-run/m-p/116830#M9185</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Wes.&amp;nbsp; That worked great.&amp;nbsp; I didn't know whether I could have a try-except embedded in another try-except, but that did the trick.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So, although this is already within a try-except block, so my indentation isn't really showing it right, this is that code block now.&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Sorry, formatting for python with this snippet was trying to make it a table.....so, snapshot..&lt;/P&gt;&lt;P&gt;&lt;IMG alt="" class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/150326_pastedImage_3.png" style="max-width: 1200px; max-height: 900px;" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 01 Dec 2015 02:43:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-test-to-determine-if-script-is-being-run/m-p/116830#M9185</guid>
      <dc:creator>RebeccaStrauch__GISP</dc:creator>
      <dc:date>2015-12-01T02:43:20Z</dc:date>
    </item>
    <item>
      <title>Re: How can I test to determine if script is being run in ArcMap vs ArcCatalog?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-test-to-determine-if-script-is-being-run/m-p/116831#M9186</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ahhh... the good old &lt;A href="http://mike.pirnat.com/2009/05/09/the-diaper-pattern-stinks/" rel="nofollow noopener noreferrer" target="_blank"&gt;diaper pattern&lt;/A&gt;​... &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Just in case there's some other exceptions waiting to bite you, you could do something like:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT")
except RuntimeError:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Not using ArcMap')
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; df&amp;nbsp; = arcpy.mapping.ListDataFrames(mxd)[0] 
&amp;nbsp;&amp;nbsp;&amp;nbsp; ddLayer = arcpy.mapping.Layer(finalOutdd) 
&amp;nbsp;&amp;nbsp;&amp;nbsp; projLayer = arcpy.mapping.Layer(finalOutProj) 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddLayer(df, ddLayer, "TOP") 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddLayer(df, projLayer, "TOP") &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:52:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-test-to-determine-if-script-is-being-run/m-p/116831#M9186</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2021-12-11T06:52:16Z</dc:date>
    </item>
    <item>
      <title>Re: How can I test to determine if script is being run in ArcMap vs ArcCatalog?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-test-to-determine-if-script-is-being-run/m-p/116832#M9187</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Luke,&lt;/P&gt;&lt;P&gt;yep.....I'm still not a very elegant programmer.&amp;nbsp;&amp;nbsp; &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&amp;nbsp; But I like that, and took the advice.&amp;nbsp; Tested with the change...works....s and it does look better. Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 01 Dec 2015 03:11:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-test-to-determine-if-script-is-being-run/m-p/116832#M9187</guid>
      <dc:creator>RebeccaStrauch__GISP</dc:creator>
      <dc:date>2015-12-01T03:11:17Z</dc:date>
    </item>
    <item>
      <title>Re: How can I test to determine if script is being run in ArcMap vs ArcCatalog?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-test-to-determine-if-script-is-being-run/m-p/116833#M9188</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Oh neither am I &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;SPAN class="emoticon_happy emoticon-inline"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I didn't know that had a name until &lt;A _jive_internal="true" data-containerid="-1" data-containertype="-1" data-objectid="3116" data-objecttype="3" href="https://community.esri.com/people/Dan_Patterson"&gt;Dan Patterson&lt;/A&gt; posted &lt;A _jive_internal="true" data-containerid="2145" data-containertype="14" data-objectid="571113" data-objecttype="2" href="https://community.esri.com/message/571113#571113"&gt;How to make mistakes in Python... a useful link&lt;/A&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 01 Dec 2015 03:32:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-test-to-determine-if-script-is-being-run/m-p/116833#M9188</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2015-12-01T03:32:00Z</dc:date>
    </item>
    <item>
      <title>Re: How can I test to determine if script is being run in ArcMap vs ArcCatalog?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-test-to-determine-if-script-is-being-run/m-p/116834#M9189</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I saw Dan's post.....on my reading list for this weekend.&amp;nbsp; &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/grin.png" /&gt;&amp;nbsp; Looks like an interesting read.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 01 Dec 2015 14:44:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-test-to-determine-if-script-is-being-run/m-p/116834#M9189</guid>
      <dc:creator>RebeccaStrauch__GISP</dc:creator>
      <dc:date>2015-12-01T14:44:17Z</dc:date>
    </item>
    <item>
      <title>Re: How can I test to determine if script is being run in ArcMap vs ArcCatalog?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-test-to-determine-if-script-is-being-run/m-p/116835#M9190</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Or, you could try this (from Jason Sheirer's entry in &lt;EM&gt;GIS Stack Exchange&lt;/EM&gt;, &lt;A href="http://gis.stackexchange.com/questions/64937/checking-if-python-script-is-run-from-arcgis-arcmap-or-on-server-or-a-stand-al" title="http://gis.stackexchange.com/questions/64937/checking-if-python-script-is-run-from-arcgis-arcmap-or-on-server-or-a-stand-al"&gt;http://gis.stackexchange.com/questions/64937/checking-if-python-script-is-run-from-arcgis-arcmap-or-on-server-or-a-stand-al&lt;/A&gt; &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the Python window (if you're in ArcMap):&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&amp;gt;&amp;gt;&amp;gt; import sys&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&amp;gt;&amp;gt;&amp;gt; sys.executable&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;CODE&gt;'C:\\Program Files (x86)\\ArcGIS\\Desktop10.3\\bin\\ArcMap.exe'&lt;/CODE&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 01 Apr 2016 00:01:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-test-to-determine-if-script-is-being-run/m-p/116835#M9190</guid>
      <dc:creator>RobertHarmon</dc:creator>
      <dc:date>2016-04-01T00:01:37Z</dc:date>
    </item>
  </channel>
</rss>

