<?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: arcpy layer.dataSource property contains exception in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-layer-datasource-property-contains-exception/m-p/1131365#M63423</link>
    <description>&lt;P&gt;Thanks, that should work, and is definitely cleaner than a try/catch. Still feels like the best way to model this is to create inherited classes for feature, raster, and other layer types that only have relevant properties. Just seems like it's too easy to write valid code that compiles and get an exception, but I'm probably shouting into the wind on this.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Semi-related - what's the best way to get the layer type for a layer in an APRX map? There's a whole bunch of fields for isFeatureLayer, isRasterLayer, isBasemapLayer, etc., but is there a simple way to get a property for the layer type without playing 20 questions? thanks!&lt;/P&gt;</description>
    <pubDate>Thu, 06 Jan 2022 19:56:48 GMT</pubDate>
    <dc:creator>davedoesgis</dc:creator>
    <dc:date>2022-01-06T19:56:48Z</dc:date>
    <item>
      <title>arcpy layer.dataSource property contains exception</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-layer-datasource-property-contains-exception/m-p/1131055#M63407</link>
      <description>&lt;P&gt;I am trying to loop through the layers in my APRX and print out their data source. When I get to an anno layer, everything looks good, but the next layer is a subclass of that anno layer and gives me an error. Imagine I am looping through a list of layers like this:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for m in aprx.listMaps():
    for lyr in m.listLayers():
        print(lyr.name)
        print(lyr.dataSource)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For an anno class layer, I am getting this&amp;nbsp;&lt;FONT face="courier new,courier" color="#993300"&gt;NameError &lt;/FONT&gt;exception on the last line.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Traceback (most recent call last):&lt;BR /&gt;File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\arcobjects\_base.py", line 90, in _get&lt;BR /&gt;return convertArcObjectToPythonObject(getattr(self._arc_object, attr_name))&lt;BR /&gt;AttributeError&lt;BR /&gt;During handling of the above exception, another exception occurred:&lt;BR /&gt;Traceback (most recent call last):&lt;BR /&gt;File "C:\eclipse\plugins\org.python.pydev.core_7.6.0.202006041357\pysrc\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec&lt;BR /&gt;exec(exp, global_vars, local_vars)&lt;BR /&gt;File "&amp;lt;console&amp;gt;", line 1, in &amp;lt;module&amp;gt;&lt;BR /&gt;File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\arcobjects\_base.py", line 96, in _get&lt;BR /&gt;(attr_name, self.__class__.__name__))&lt;BR /&gt;NameError: The attribute 'dataSource' is not supported on this instance of Layer.&lt;/PRE&gt;&lt;P&gt;If I put my IDE into debug mode and stop on a breakpoint, I'm seeing some &lt;EM&gt;&lt;U&gt;really&lt;/U&gt; &lt;/EM&gt;odd stuff. T&lt;SPAN&gt;he &lt;FONT face="courier new,courier"&gt;dataSource&lt;/FONT&gt; is there, and is of type &lt;/SPAN&gt;&lt;FONT face="courier new,courier"&gt;str&lt;/FONT&gt;&lt;SPAN&gt;. The value of the property is exactly the Traceback message I see at the console. I've never seen an object's property have an exception locked and loaded this way (before I even try to access it), and I am super puzzled at the property being type &lt;FONT face="courier new,courier"&gt;str&lt;/FONT&gt;.&amp;nbsp; Any attempt to use the property, like&amp;nbsp;&lt;/SPAN&gt;&lt;FONT face="courier new,courier" color="#993300"&gt;print(lyr.dataSource)&lt;/FONT&gt;&lt;SPAN&gt;, raises the &lt;/SPAN&gt;&lt;FONT face="courier new,courier"&gt;NameError&lt;/FONT&gt;&lt;SPAN&gt; exception. I even get the exception when I try to test for it:&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;hasattr(lyr, 'dataSource')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NOTE: There are several other &lt;FONT face="courier new,courier"&gt;str &lt;/FONT&gt;properties in this object with this exception information in them, such as &lt;FONT face="courier new,courier"&gt;brightness&lt;FONT face="arial,helvetica,sans-serif"&gt;,&lt;/FONT&gt; connectionProperties&lt;/FONT&gt;, and&amp;nbsp;&lt;FONT face="courier new,courier"&gt;definitionQuery&lt;/FONT&gt;.&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;QUESTIONS:&amp;nbsp;&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;1.&lt;/STRONG&gt; What is the best way to solve this? I can test for this using a try/except block and trap &lt;FONT face="courier new,courier"&gt;NameError&lt;/FONT&gt;. That works, but it feels like forcing a failure and catching it is not Pythonic. Is there a better way?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;2.&lt;/STRONG&gt; Looking beyond solving the problem, I'd like to learn how a class's property can store an exception like this. I tried to create my own class with a property containing the exact same &lt;FONT face="courier new,courier"&gt;str&lt;/FONT&gt; value. Python just treats that property like regular text and does not raise a &lt;FONT face="courier new,courier"&gt;NameError&lt;/FONT&gt; exception when I work with that property. I personally don't think what Esri is doing here is Pythonic, and suspect it isn't even intentional, but I am curious to know how one puts an exception into a property when it is instantiated. I can't think of a use case for this, but I'm always down to learn more Python.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jan 2022 00:03:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-layer-datasource-property-contains-exception/m-p/1131055#M63407</guid>
      <dc:creator>davedoesgis</dc:creator>
      <dc:date>2022-01-06T00:03:02Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy layer.dataSource property contains exception</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-layer-datasource-property-contains-exception/m-p/1131070#M63408</link>
      <description>&lt;P&gt;not all layer types support all properties, you have to follow the trail&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/layer-class.htm" target="_blank" rel="noopener"&gt;Layer—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;and test for it&lt;/P&gt;&lt;PRE&gt;supports (layer_property)&lt;/PRE&gt;&lt;P&gt;Specifically&lt;/P&gt;&lt;P&gt;DATASOURCE—A layer's file path or connection file.&lt;/P&gt;&lt;P&gt;So sometimes you need to test the layer to see if it supports property you want to get/set first&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jan 2022 01:54:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-layer-datasource-property-contains-exception/m-p/1131070#M63408</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2022-01-06T01:54:17Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy layer.dataSource property contains exception</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-layer-datasource-property-contains-exception/m-p/1131365#M63423</link>
      <description>&lt;P&gt;Thanks, that should work, and is definitely cleaner than a try/catch. Still feels like the best way to model this is to create inherited classes for feature, raster, and other layer types that only have relevant properties. Just seems like it's too easy to write valid code that compiles and get an exception, but I'm probably shouting into the wind on this.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Semi-related - what's the best way to get the layer type for a layer in an APRX map? There's a whole bunch of fields for isFeatureLayer, isRasterLayer, isBasemapLayer, etc., but is there a simple way to get a property for the layer type without playing 20 questions? thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jan 2022 19:56:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-layer-datasource-property-contains-exception/m-p/1131365#M63423</guid>
      <dc:creator>davedoesgis</dc:creator>
      <dc:date>2022-01-06T19:56:48Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy layer.dataSource property contains exception</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-layer-datasource-property-contains-exception/m-p/1131429#M63427</link>
      <description>&lt;P&gt;the CIM is evolving based on user needs, but it won't be a replacement for Pro's user interface anytime soon&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jan 2022 22:00:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-layer-datasource-property-contains-exception/m-p/1131429#M63427</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2022-01-06T22:00:58Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy layer.dataSource property contains exception</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-layer-datasource-property-contains-exception/m-p/1133128#M63491</link>
      <description>&lt;P&gt;Dan's suggestion of using the &lt;FONT face="courier new,courier" color="#993300"&gt;supports('&lt;EM&gt;&amp;lt;layer_property&amp;gt;&lt;/EM&gt;')&lt;/FONT&gt; check is the most correct way to do this. It still feels like the most Pythonic implementation would have been for each type of layer to have its own class, but that would create much more introspection and checking than the current approach. So score one for pragmatic solutions over Pythonic solutions.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;NOTE:&lt;/STRONG&gt; There is at least one property I've discovered that does not accurately work with the &lt;FONT face="courier new,courier" color="#993300"&gt;supports()&lt;/FONT&gt; function: the &lt;EM&gt;&lt;FONT face="courier new,courier" color="#993300"&gt;showLabels&lt;/FONT&gt;&lt;/EM&gt; property.&amp;nbsp;&lt;/P&gt;&lt;P&gt;When a layer supports labels and labels are turned on, it works as expected:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;&amp;gt;&amp;gt;&amp;gt; lyr.supports('showLabels')
True
&amp;gt;&amp;gt;&amp;gt; lyr.showLabels
True&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When a layer does not support labels, it also works as expected, raising a NameError exception if you try to use that property:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;&amp;gt;&amp;gt;&amp;gt; lyr.supports('showLabels')
False
&amp;gt;&amp;gt;&amp;gt; lyr.showLabels  # THIS SHOULD RAISE AN EXCEPTION, AND IT DOES
Traceback (most recent call last):
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\arcobjects\_base.py", line 90, in _get
    return convertArcObjectToPythonObject(getattr(self._arc_object, attr_name))
AttributeError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\eclipse\plugins\org.python.pydev.core_7.6.0.202006041357\pysrc\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "&amp;lt;console&amp;gt;", line 1, in &amp;lt;module&amp;gt;
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\arcobjects\_base.py", line 96, in _get
    (attr_name, self.__class__.__name__))
NameError: The attribute 'showLabels' is not supported on this instance of Layer.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;U&gt;BUT&lt;/U&gt;&lt;/EM&gt;, if a layer supports labeling, but just happens to have the labels turned off, it returns False for the supports() function:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;&amp;gt;&amp;gt;&amp;gt; # THIS SHOULD BE TRUE; THE LAYER SUPPORTS LABELS, EVEN THOUGH THEY ARE OFF
&amp;gt;&amp;gt;&amp;gt; lyr.supports('showLabels')  
False
&amp;gt;&amp;gt;&amp;gt; # BUT NOW THAT supports() IS FALSE, THIS SHOULD RAISE AN EXCEPTION
&amp;gt;&amp;gt;&amp;gt; lyr.showLabels 
False&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This may seem like a trivial thing, but it means I still have to put my code in a try/except block and force an exception to find out if showLabels is supported.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regarding my question #2, my best guess is that Esri is not pre-loading those exceptions into the properties. The more I think about it, my Python IDE must be trying to ping all the properties to fill in their variables, and so the API returns those exceptions at the moment my IDE enters debug mode. I guess my IDE stringifies that NameError, leading to what I see.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jan 2022 22:39:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-layer-datasource-property-contains-exception/m-p/1133128#M63491</guid>
      <dc:creator>davedoesgis</dc:creator>
      <dc:date>2022-01-12T22:39:09Z</dc:date>
    </item>
  </channel>
</rss>

