Select to view content in your preferred language

listMaps() returns empty list when the project has a map

1087
5
06-22-2023 07:44 AM
AngelaSchirck
Frequent Contributor

Hey all,

I'm trying to create a script and am stuck at a very SIMPLE step.  In the code below the listMaps() function is returning an empty list so I'm getting an index out of range error.  This project DOES have one map in it, any ideas why it's not "seeing" it? (All necessary packages are imported in the project).

When I print aprx I get: <arcpy._mp.ArcGISProject object at 0x0000021769973640>

When I delete the index [0] from m and print m it shows [ ] (empty list)

Thanks

```

def sdDraft(outdir, sde_path):  ## Output directory (project_path in main).
project_name = os.path.join(outdir, 'Enterprise.aprx') ## Change as needed
service_name = 'CensusTest' ## Change as needed
sddraft_filename = service_name + '.sddraft'
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
sd_filename = service_name + '.sd'
sd_output_filename = os.path.join(outdir, sd_filename)

# Layer(s) to publish
aprx = arcpy.mp.ArcGISProject(project_name)
m = aprx.listMaps('Enterprise')[0] ## Change as needed
print(m)
```

  

Tags (2)
0 Kudos
5 Replies
AlfredBaldenweck
MVP Regular Contributor

You're explicitly checking for a map named "Enterprise". Is that the name of your map?

0 Kudos
AngelaSchirck
Frequent Contributor

No, the map is called Map (but I tried that as well and didn't even get an empty list).  I also left it blank.  There seems to be no documentation for what to put in the parenthesis, I'm just going by examples.

0 Kudos
AngelaSchirck
Frequent Contributor

Hmmm, well I changed it to "Map" again and this time it didn't give an error. However, if it were left blank would it not just list all maps in the project?

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Yes.

What .listMaps() does is return a list of all maps. Adding the [0] afterwards returns the first item in the list.

So when you call .listMaps("Map"), it returns a list with only one item, ["Map]". 

.listMaps("Map")[0] returns the first item in the list of only one item.

TylerT
by
Frequent Contributor

@AngelaSchirck,

@AlfredBaldenweck is correct.  The parameter you are passing into listMaps is what ESRI calls the wildcard, although it behaves more like a regex match, seeking that exact name, except if you pass the star wildcard (see image below).  You are correct, if the wildcard is left empty, you should get a full list of all your maps.  If you are not getting a list of your maps, then something is wrong with your code (wrong path?).  Here are a few examples of how one could use the wildcard.  Notice, list maps returns maps objects, if you want names, you need to reach into the name property.  I created a list of names below using a list comprehension on the name property.  Also, notice, as @AlfredBaldenweck mentioned, the index ([0]) is just for accessing single maps (or slicing [1:3] multiple).

TylerT_0-1690209940697.png

A word of caution...ArcGIS Pro allows same name maps as seen above.  The user will have to use manual conventions to avoid this ambiguity if trying to access maps from listMaps().

HTH.

Tyler