0

I am working on updating a Python 2 script (arcpy.mapping) to a Python 3 script (arcpy.mp).

I've successfully updated the following:

aprxFile=r"G:\usr\Drinking Water Programs\Monroe 2023-02\Maps\ProMap.aprx" #aprx document
arcpy.env.overwriteOutput = True
aprx = Map.ArcGISProject(aprxFile)
layout = aprx.listLayouts('DWP_Maps_5')[0]
noteStat = layout.listElements("TEXT_ELEMENT","Note_statistic")[0]
noteStat.text = "Mapped value is the average for the "+unit
noteTreat = layout.listElements("TEXT_ELEMENT", "Note_treated")[0]
noteTreat.text = "Treated samples not mapped"
noteAny = layout.listElements("TEXT_ELEMENT", "Note_any_sample")[0]
noteAny.text = " "
notePerDist = layout.listElements("TEXT_ELEMENT", "Note_Distribution_per_unit")[0]
notePerDist.text = " "
notePerNum = layout.listElements("TEXT_ELEMENT", "Note_Numbers_per_unit")[0]
notePerNum.text = " "

The next part not so successfully:

dfList = aprx.listMaps(aprx)
baseMCD1 = dfList.listLayers("MCD_light") #new code
baseMCD2 = Map.listLayers(aprx,"MCD_medium",dfList[0])[0] #old code

I get the following error:

AttributeError Traceback (most recent call last) In [37]: Line 84:
baseMCD1 = dfList.listLayers("MCD_light")

AttributeError: 'list' object has no attribute 'listLayers'

1
  • 1
    You are trying to use arcpy.mapping sytax with arcpy.mp. For example, aprx.listMaps(aprx) isn't going to return any results. Even after you change the line to dfList = aprx.listMaps(), the next line of code still won't work because listMaps() returns a Python list of Maps in the APRX, and Python lists have no listLayers method, hence the error. You need to either select a map from the Python list or iterate over the whole list and call retrieve the layers of the individual Map objects.
    – bixb0012
    Dec 15, 2023 at 20:01

2 Answers 2

2

You are trying to listLayers on the list object of maps. You need to select a map from that list.

Example:

dfList = aprx.listMaps()[0] #First map in list
0
0

You have not included your imports but, in any event, I think you will be writing more readable code if you replace:

aprx = Map.ArcGISProject(aprxFile)

with:

import arcpy

aprx = arcpy.mp.ArcGISProject(aprxFile)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.