I'm trying to import a batch .mxds into ArcPro projects and have run into a NameError I can't figure out.
I need the script to walk some folders, import .mxds one at a time, remove some layers, add some layers, change some layers and then save a copy of each ArcPro project. Last week the script ran just fine in ArcPro 2.7. I updated to ArcPro 2.8 and the script was broken. I had IT do a clean reinstall of ArcPro but no joy.
The script is longish but it's a just a bunch of simple code strung together. The relevant part is:
aprx = arcpy.mp.ArcGISProject(r" path to blank .aprx ")
aprx.importDocument( .mxd path grabbed from folder walk )
for m in aprx.listMaps():
if m.name == "Layers":
print("Adding: {0}".format(addlyr))
print("Adding: {0}".format(addlyr2))
m.addDataFromPath(addlyr)
m.addDataFromPath(addlyr2)
for lyr in m.listLayers():
if lyr.name == "Graphics Layer":
m.removeLayer(lyr)
I have a long string of elif statements after this one. It errors on this first one, but if I remove it the error triggers on the next one and so on.
This is the error message I get:
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:
File: " my script " line 52, in <module>
Traceback (most recent call last):
if lyr.name == "Graphics Layer":
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\arcobjects\_base.py", line 96, in _get
(attr_name, self.__class__.__name__))
NameError: The attribute 'name' is not supported on this instance of Layer.
I've tried this in a separate script running from PyCharm and in an ArcPro notebook. Same error every time.
Has anybody else seen this?
It may just be a coincidence, and you have a layer which doesn't support the name property (I don't know which type of layer that would be though!).
Maybe to check this you could put in a try except
for lyr in m.listLayers():
try:
if lyr.name == "Graphics Layer":
m.removeLayer(lyr)
...
except:
print(...)
not all "layers" have the same properties. It is a good idea to check for a particular layer type prior to checking any property unless you are sure it is common to all using the "is" methods. For example
isFeatureLayer
should be used if you are checking for feature layers and they have a name
Layer—ArcGIS Pro | Documentation
The thing that brought this to my attention is the error message you got
'name' is not supported on this instance of Layer.
There are a number of different types of layers...
Just had the same thing went to Pro 2.8 and the layer.name property call is failing with this error, when it worked in 2.7.
Layer.name is a very common property to call and all Layers have historically supported this, it is there in the intellisense in the Python window, but if you try and access Layer.name you get this error 😕
so not a "you are calling a non-existent property issue", looks like a bug to me ..
.. in fact this is happening on ALL layer properties not just name, rolling back to 2.7 😢
pro = arcpy.mp.ArcGISProject('CURRENT')
map = pro.activeMap
for lx in map.listLayers() :
if (lx.visible): pass
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 "<string>", line 5, in <module>
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\arcobjects\_base.py", line 96, in _get
(attr_name, self.__class__.__name__))
NameError: The attribute 'visible' is not supported on this instance of Layer.
I had this same issue. No need to downgrade to 2.7 as I believe this is intended behavior. I added the try except block that David Pike suggested and that fixed the issue. I guess not all layers have a name property. Seems weird but my tool is working again.
Update: I ended up changing it to "if lyr.supports("NAME"):" instead of the try/except because that seems like a better way to handle the situation.
As a follow-up to this I eventually got the script to work by inserting "if lyr.supports("NAME"):" which then allowed the script to run.
Like I mentioned above the original script failed even if I included only layers that I was certain supported the name property. I haven't had the opportunity to use the name property in a script in a different situation so I don't know if this was a one-off quirk. I do know that my original script ran exactly as I wanted before my upgrade to ArcPro 2.8.