ArcGIS Pro: layer.supports...the check works, then fails when trying to read that same property!??!?

532
3
09-07-2021 10:08 PM
TimothyBarnes
Occasional Contributor

for lyr in updateMap.listLayers():
     if lyr.supports("DATASOURCE"):
         arcpy.AddMessage(lyr.dataSource)

A script opens an ArcGIS Pro project file, goes through the list of layers within a map, checks to see if each layer supports the 'DataSource' property before giving me a message with that datasource

The script fails as it reaches a layer which doesn't support the 'DataSource' property....

NameError: The attribute 'dataSource' is not supported on this instance of Layer.

Can someone explain this non-sensical behaviour to me?

ArcGIS Pro 2.8.2

Tags (3)
0 Kudos
3 Replies
JohannesLindner
MVP Regular Contributor

I have no idea why this happens, but to not let it fail your script, you can do it in a try-catch block:

for lyr in updateMap.listLayers():
    try:
        arcpy.AddMessage(lyr.dataSource)
    except NameError:
        pass

Have a great day!
Johannes
DanPatterson
MVP Esteemed Contributor

from Layer—ArcGIS Pro | Documentation

could it be your layer source?

dataSource
(Read Only)

Returns the complete path for the layer's data source. It includes the full workspace path and name of the dataset. For enterprise geodatabase layers, a string containing the layer's connection information is returned.

Tip:

Enterprise geodatabase layers in an ArcGIS Pro project do not retain the path to the database connection file (.sde) that was used to create the layer.

 

Strin

... sort of retired...
0 Kudos
TimothyBarnes
Occasional Contributor

Possible (and I'll check) but not likely as all the layers that I check (using a query on the list layers) are in the same location (default.gdb)...but even then, the whole point of 'supports' is that this type of error doesn't occur.

0 Kudos