<?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: Adding layer created using Python to the active data frame will not work in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652598#M50822</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Gerry what is the error message giving you exactly? &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 26 Oct 2015 15:39:18 GMT</pubDate>
    <dc:creator>LukeSturtevant</dc:creator>
    <dc:date>2015-10-26T15:39:18Z</dc:date>
    <item>
      <title>Adding layer created using Python to the active data frame will not work</title>
      <link>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652591#M50815</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have Python code that uses a Model Builder Feature Set as an input to prompt the user to manually create a line inside the data view of the active data frame.&amp;nbsp; Then Python code calculates the grid north azimuth and the true north azimuth of the line and prints those values to the geoprocessing results window.&amp;nbsp; The feature set (and code) create in_memory data using CreateFeatureclass_management....That all works fine but what I want to do is add the in_memory data to the active data frame.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I can get the name of the data frame using mxd.activeDataFrame.name but adding the layer is not working.&amp;nbsp; See my code around line 72 - 74.&lt;/P&gt;&lt;P&gt;Any help is appreciated.&lt;/P&gt;&lt;P&gt;&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; import sys, traceback
&amp;nbsp;&amp;nbsp;&amp;nbsp; import arcpy
&amp;nbsp;&amp;nbsp;&amp;nbsp; import time, math
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("\nGet the Azimuth From A User Defined Line.")
&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Created by Gerry Gabrisch GISP, GIS Manager Lummi Indian Business Council, &lt;/SPAN&gt;&lt;A class="jive-link-email-small" href="mailto:geraldg@lummi-nsn.gov" rel="nofollow noopener noreferrer" target="_blank"&gt;geraldg@lummi-nsn.gov&lt;/A&gt;&lt;SPAN&gt;\n")&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.overwriteOutput = True
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; inFC = arcpy.GetParameterAsText(0)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; outFL = "outFL"
&amp;nbsp;&amp;nbsp;&amp;nbsp; outlayer = "outlayer"
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(inFC, outFL)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SaveToLayerFile_management(outFL, outlayer)

&amp;nbsp;&amp;nbsp;&amp;nbsp; def cart2pol(x, y):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rho = math.sqrt(x**2 + y**2)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; phi = math.atan2(y, x)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return(rho, phi)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; def UnitCircleDegreesToTrueNorthAzimuth(theta):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; '''Converts Arithmatic Degrees (East = 0 then counter clockwise)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; to Geographic Degrees (North = 0 then clockwise).'''
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; import math
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #theta = math.degrees(theta)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; theta = theta - 90.0
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if theta &amp;lt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; theta = theta + 360.0
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; theta = -1*(theta * 2 * math.pi / 360.0)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 360.0 + math.degrees(theta)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in arcpy.da.SearchCursor(inFC, ["OID@", "SHAPE@"]):
&amp;nbsp;&amp;nbsp;&amp;nbsp; #Set start point
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; startpt = row[1].firstPoint
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; startX = startpt.X
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; startY = startpt.Y
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; endpt = row[1].lastPoint
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; endX = endpt.X
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; endY = endpt.Y
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Normalizing coordinates...")
&amp;nbsp;&amp;nbsp;&amp;nbsp; x = endX - startX
&amp;nbsp;&amp;nbsp;&amp;nbsp; y = endY - startY
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Radians, degrees, and grid convergence, oh my!\n")
&amp;nbsp;&amp;nbsp;&amp;nbsp; pol = cart2pol(x, y)
&amp;nbsp;&amp;nbsp;&amp;nbsp; degs = math.degrees(pol[1])
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; azi = UnitCircleDegreesToTrueNorthAzimuth(degs)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT")
&amp;nbsp;&amp;nbsp;&amp;nbsp; spacRef = mxd.activeDataFrame.spatialReference
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; tempFC = arcpy.CreateFeatureclass_management("in_memory", "tempFC", geometry_type = "POINT", spatial_reference = spacRef )
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(tempFC, "angle", "DOUBLE")
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor = arcpy.da.InsertCursor(tempFC, ["SHAPE@XY"])
&amp;nbsp;&amp;nbsp;&amp;nbsp; xy = (startX, startY)
&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor.insertRow([xy])
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateGridConvergenceAngle_cartography(tempFC, "angle")
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in arcpy.da.SearchCursor(tempFC, ["angle"]):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; convergence = row[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Line azimuth from grid north in degrees = " + str(azi))
&amp;nbsp;&amp;nbsp;&amp;nbsp; azi = str(azi + convergence)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Line azimuth from true north in degrees= " + azi)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddLayer(mxd.activeDataFrame.name, tempFC, "TOP")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView()
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshTOC()
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; time.sleep(10)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("\nClosing")

except arcpy.ExecuteError: 
&amp;nbsp;&amp;nbsp;&amp;nbsp; msgs = arcpy.GetMessages(2) 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError(msgs)&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(msgs)
except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; tb = sys.exc_info()[2]
&amp;nbsp;&amp;nbsp;&amp;nbsp; tbinfo = traceback.format_tb(tb)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
&amp;nbsp;&amp;nbsp;&amp;nbsp; print pymsg


&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 03:40:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652591#M50815</guid>
      <dc:creator>GerryGabrisch</dc:creator>
      <dc:date>2021-12-12T03:40:00Z</dc:date>
    </item>
    <item>
      <title>Re: Adding layer created using Python to the active data frame will not work</title>
      <link>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652592#M50816</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;you have 2 except lines but I can only find one try&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Oct 2015 15:14:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652592#M50816</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2015-10-26T15:14:31Z</dc:date>
    </item>
    <item>
      <title>Re: Adding layer created using Python to the active data frame will not work</title>
      <link>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652593#M50817</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The &lt;A href="http://resources.arcgis.com/EN/HELP/MAIN/10.2/index.html#//00s300000025000000"&gt;Add Layer&lt;/A&gt;​ function looks for a data frame object not a data frame name. You should be able to just change your code to:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14458728055169255 jive_text_macro" data-renderedposition="50_8_912_16" jivemacro_uid="_14458728055169255"&gt;&lt;P&gt;arcpy.mapping.AddLayer(mxd.activeDataFrame, tempFC,"TOP")&lt;/P&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, sometimes you need to explicitly set the layer to a full path like so:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="_jivemacro_uid_14458728132686034 jive_macro_code jive_text_macro" data-renderedposition="129_8_912_16" jivemacro_uid="_14458728132686034"&gt;&lt;P&gt;arcpy.mapping.AddLayer(mxd.activeDataFrame, &lt;SPAN class="string" style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: blue;"&gt;"in_memory\\&lt;/SPAN&gt;&lt;SPAN class="string" style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: blue;"&gt;tempFC"&lt;/SPAN&gt;,"TOP")&lt;/P&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Oct 2015 15:17:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652593#M50817</guid>
      <dc:creator>LukeSturtevant</dc:creator>
      <dc:date>2015-10-26T15:17:35Z</dc:date>
    </item>
    <item>
      <title>Re: Adding layer created using Python to the active data frame will not work</title>
      <link>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652594#M50818</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can have multiple except lines within a single try statement. That should not effect the code.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Oct 2015 15:19:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652594#M50818</guid>
      <dc:creator>LukeSturtevant</dc:creator>
      <dc:date>2015-10-26T15:19:18Z</dc:date>
    </item>
    <item>
      <title>Re: Adding layer created using Python to the active data frame will not work</title>
      <link>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652595#M50819</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Luke, sorry, nope, neither solutions worked..&lt;/P&gt;&lt;P&gt;arcpy.mapping.AddLayer(mxd.activeDataFrame, tempFC,&lt;SPAN class="string"&gt;"TOP")&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;arcpy.mapping.AddLayer(mxd.activeDataFrame, &lt;SPAN class="string"&gt;"in_memory\\tempFC",&lt;SPAN class="string"&gt;"TOP"&lt;/SPAN&gt;) &lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;...but you are right about the two different error handlers.&amp;nbsp; I have been using that method (copied from the ESRI arcpy examples) for years.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Oct 2015 15:31:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652595#M50819</guid>
      <dc:creator>GerryGabrisch</dc:creator>
      <dc:date>2015-10-26T15:31:15Z</dc:date>
    </item>
    <item>
      <title>Re: Adding layer created using Python to the active data frame will not work</title>
      <link>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652596#M50820</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hmmmm tested in python 3.4&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; import ArCpY
except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print("such a failure")&lt;/PRE&gt;&lt;P&gt;result&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; such a failure&lt;/PRE&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; import ArCpY
except:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # with or without this colon
&amp;nbsp;&amp;nbsp;&amp;nbsp; print("such a failure")
except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print("I repeat")&lt;/PRE&gt;&lt;P&gt;Failed to check - syntax error -default 'except:' must be last&lt;/P&gt;&lt;P&gt;hmmmmm&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; import ArCpY
except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print("such a failure")
finally:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print("I repeat")&lt;/PRE&gt;&lt;P&gt;result&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;such a failure
I repeat&lt;/PRE&gt;&lt;P&gt;What version are you using?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 03:40:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652596#M50820</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-12T03:40:03Z</dc:date>
    </item>
    <item>
      <title>Re: Adding layer created using Python to the active data frame will not work</title>
      <link>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652597#M50821</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Python 2.7. It looks like that functionality is either redundant or did not follow over to 3.4. That's too bad. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Oct 2015 15:38:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652597#M50821</guid>
      <dc:creator>LukeSturtevant</dc:creator>
      <dc:date>2015-10-26T15:38:23Z</dc:date>
    </item>
    <item>
      <title>Re: Adding layer created using Python to the active data frame will not work</title>
      <link>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652598#M50822</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Gerry what is the error message giving you exactly? &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Oct 2015 15:39:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652598#M50822</guid>
      <dc:creator>LukeSturtevant</dc:creator>
      <dc:date>2015-10-26T15:39:18Z</dc:date>
    </item>
    <item>
      <title>Re: Adding layer created using Python to the active data frame will not work</title>
      <link>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652599#M50823</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Dan, I am on ArcGIS 10.3 with Python 2.7&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is the link to the ESRI help from ArcGIS 10&lt;/P&gt;&lt;P&gt;&lt;A href="http://help.arcgis.com/EN/arcgisdesktop/10.0/help/index.html#//002z0000000q000000" title="http://help.arcgis.com/EN/arcgisdesktop/10.0/help/index.html#//002z0000000q000000"&gt;ArcGIS Desktop&lt;/A&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I did alter the code to have only one except statement but that was not the cause of the troubles...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Oct 2015 15:42:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652599#M50823</guid>
      <dc:creator>GerryGabrisch</dc:creator>
      <dc:date>2015-10-26T15:42:02Z</dc:date>
    </item>
    <item>
      <title>Re: Adding layer created using Python to the active data frame will not work</title>
      <link>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652600#M50824</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Oh wait I didn't notice this before, but in addition to changing to a data frame object you also need to generate a mapping layer object to add to your TOC first:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;lyr = arcpy.mapping.Layer(&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;tempFC&lt;/SPAN&gt;)&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;arcpy.mapping.AddLayer(mxd.activeDataFrame, lyr ,&lt;/SPAN&gt;&lt;SPAN class="string" style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;"TOP")&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="string" style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;Hopefully that will solve the issue.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Oct 2015 15:42:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652600#M50824</guid>
      <dc:creator>LukeSturtevant</dc:creator>
      <dc:date>2015-10-26T15:42:05Z</dc:date>
    </item>
    <item>
      <title>Re: Adding layer created using Python to the active data frame will not work</title>
      <link>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652601#M50825</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I don't get any errors, the code executes fine, but nothing is added to the TOC.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Oct 2015 15:42:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652601#M50825</guid>
      <dc:creator>GerryGabrisch</dc:creator>
      <dc:date>2015-10-26T15:42:44Z</dc:date>
    </item>
    <item>
      <title>Re: Adding layer created using Python to the active data frame will not work</title>
      <link>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652602#M50826</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Luke, shouldn't that be:&lt;/P&gt;&lt;P&gt;lyr = arcpy.mapping.Layer(&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;tempFC&lt;/SPAN&gt;)&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;arcpy.mapping.AddLayer(mxd.activeDataFrame, lyr,&lt;/SPAN&gt;&lt;SPAN class="string" style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;"TOP")&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="string" style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="string" style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;In any case, it is still not working.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Oct 2015 15:47:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652602#M50826</guid>
      <dc:creator>GerryGabrisch</dc:creator>
      <dc:date>2015-10-26T15:47:12Z</dc:date>
    </item>
    <item>
      <title>Re: Adding layer created using Python to the active data frame will not work</title>
      <link>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652603#M50827</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes you are correct. I should have referenced the layer object. Did you try this yet:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;lyr = arcpy.mapping.Layer(&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;"in_memory\\tempFC")&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit;"&gt;arcpy.mapping.AddLayer(mxd.activeDataFrame, lyr,&lt;/SPAN&gt;&lt;SPAN class="string" style="font-weight: inherit; font-style: inherit;"&gt;"TOP")&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Oct 2015 15:50:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652603#M50827</guid>
      <dc:creator>LukeSturtevant</dc:creator>
      <dc:date>2015-10-26T15:50:58Z</dc:date>
    </item>
    <item>
      <title>Re: Adding layer created using Python to the active data frame will not work</title>
      <link>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652604#M50828</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Dan, just out of curiosity, did you try a double except statement&amp;nbsp; with the first except block invoking a specific error set up similar to Gerry's except arcpy.ExecuteError: ?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Oct 2015 15:55:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652604#M50828</guid>
      <dc:creator>LukeSturtevant</dc:creator>
      <dc:date>2015-10-26T15:55:41Z</dc:date>
    </item>
    <item>
      <title>Re: Adding layer created using Python to the active data frame will not work</title>
      <link>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652605#M50829</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Worked!&amp;nbsp; Thanks, Luke!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Oct 2015 17:03:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652605#M50829</guid>
      <dc:creator>GerryGabrisch</dc:creator>
      <dc:date>2015-10-26T17:03:32Z</dc:date>
    </item>
    <item>
      <title>Re: Adding layer created using Python to the active data frame will not work</title>
      <link>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652606#M50830</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It is still there but slight changes&lt;/P&gt;&lt;P&gt;You have to do it like this now explicitly&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN class="kn"&gt;import&lt;/SPAN&gt; &lt;SPAN class="nn"&gt;sys&lt;/SPAN&gt;
&lt;SPAN class="k"&gt;def&lt;/SPAN&gt; &lt;SPAN class="nf"&gt;divide&lt;/SPAN&gt;&lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="n"&gt;x&lt;/SPAN&gt;&lt;SPAN class="p"&gt;,&lt;/SPAN&gt; &lt;SPAN class="n"&gt;y&lt;/SPAN&gt;&lt;SPAN class="p"&gt;):&lt;/SPAN&gt;
&lt;SPAN class="gp"&gt;...&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN class="k"&gt;try&lt;/SPAN&gt;&lt;SPAN class="p"&gt;:&lt;/SPAN&gt;
&lt;SPAN class="gp"&gt;... &lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="n"&gt;result&lt;/SPAN&gt; &lt;SPAN class="o"&gt;=&lt;/SPAN&gt; &lt;SPAN class="n"&gt;x&lt;/SPAN&gt; &lt;SPAN class="o"&gt;/&lt;/SPAN&gt; &lt;SPAN class="n"&gt;y&lt;/SPAN&gt;
&lt;SPAN class="gp"&gt;... &lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="k"&gt;except&lt;/SPAN&gt; &lt;SPAN class="ne"&gt;ZeroDivisionError&lt;/SPAN&gt;&lt;SPAN class="p"&gt;:&lt;/SPAN&gt;
&lt;SPAN class="gp"&gt;... &lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="nb"&gt;print&lt;/SPAN&gt;&lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="s"&gt;"division by zero!"&lt;/SPAN&gt;&lt;SPAN class="p"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="gp"&gt;...&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN class="k"&gt;else&lt;/SPAN&gt;&lt;SPAN class="p"&gt;:&lt;/SPAN&gt;
&lt;SPAN class="gp"&gt;... &lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="nb"&gt;print&lt;/SPAN&gt;&lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="s"&gt;"result is"&lt;/SPAN&gt;&lt;SPAN class="p"&gt;,&lt;/SPAN&gt; &lt;SPAN class="n"&gt;result&lt;/SPAN&gt;&lt;SPAN class="p"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="gp"&gt;...&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN class="k"&gt;finally&lt;/SPAN&gt;&lt;SPAN class="p"&gt;:&lt;/SPAN&gt;
&lt;SPAN class="gp"&gt;... &lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="nb"&gt;print&lt;/SPAN&gt;&lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="s"&gt;"executing finally clause"&lt;/SPAN&gt;&lt;SPAN class="p"&gt;)&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;So for the most part, nothing changes except I can't get anything to print after an arcpy.ExecuteError.&lt;/P&gt;&lt;P&gt;I never use try except blocks .&amp;nbsp; I find them less informative than an actual error message, then I clean up using locals().keys() to find then delete any namespace.&amp;nbsp; The only upside with Pro is RefreshActiveView is no longer needed with the new mp module&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 03:40:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652606#M50830</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-12T03:40:06Z</dc:date>
    </item>
    <item>
      <title>Re: Adding layer created using Python to the active data frame will not work</title>
      <link>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652607#M50831</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Multiple except clauses work fine as long as the catch-all "except:" statement is last.&amp;nbsp; From the 2.7 Python docs on &lt;A href="https://docs.python.org/2/tutorial/errors.html#handling-exceptions"&gt;Handling Exceptions&lt;/A&gt;, which is the same as the 3.5 docs:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;SPAN style="text-align: justify; color: #000000; text-indent: 0px;"&gt;A&lt;SPAN class="Apple-converted-space"&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;A class="reference internal" href="https://docs.python.org/2/reference/compound_stmts.html#try" style="text-align: justify; color: #355f7c; text-indent: 0px;"&gt;&lt;TT class="docutils std xref std-keyword literal" style="padding: 0px 1px; font-size: 0.95em; font-weight: bold;"&gt;&lt;SPAN class="pre"&gt;try&lt;/SPAN&gt;&lt;/TT&gt;&lt;/A&gt;&lt;SPAN style="text-align: justify; color: #000000; text-indent: 0px;"&gt;&lt;SPAN class="Apple-converted-space"&gt; &lt;/SPAN&gt;statement may have more than one except clause, to specify handlers for different exceptions. At most one handler will be executed. Handlers only handle exceptions that occur in the corresponding try clause, not in other handlers of the same&lt;SPAN class="Apple-converted-space"&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;A class="reference internal" href="https://docs.python.org/2/reference/compound_stmts.html#try" style="text-align: justify; color: #355f7c; text-indent: 0px;"&gt;&lt;TT class="docutils std xref std-keyword literal" style="padding: 0px 1px; font-size: 0.95em; font-weight: bold;"&gt;&lt;SPAN class="pre"&gt;try&lt;/SPAN&gt;&lt;/TT&gt;&lt;/A&gt;&lt;SPAN style="text-align: justify; color: #000000; text-indent: 0px;"&gt;&lt;SPAN class="Apple-converted-space"&gt; &lt;/SPAN&gt;statement.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="text-align: justify; color: #000000; text-indent: 0px;"&gt;....&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; text-indent: 0px; text-align: justify;"&gt;The last except clause may omit the exception name(s), to serve as a wildcard. Use this with extreme caution, since it is easy to mask a real programming error in this way! It can also be used to print an error message and then re-raise the exception (allowing a caller to handle the exception as well):&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Oct 2015 20:42:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-layer-created-using-python-to-the-active/m-p/652607#M50831</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2015-10-26T20:42:09Z</dc:date>
    </item>
  </channel>
</rss>

