Select to view content in your preferred language

Python 2 to 3 Upgrade - Help with listLayers

06-10-2019 09:07 PM
by Anonymous User
Not applicable

I was wondering if anyone could help me today...

My organisation has recently realised that Python 2.7 is dying this year and are looking at updating any old scripts to Python 3 and/or Pro, and I've been having some issues with the listLayers function.

Caveat: I've run the Python 2to3 script.

Basically I have a script that creates images from a .MXD pointed at a .GDB. Everything else seems to be working/has been converted or I already fixed but for the life of me, I can't get the ListLayers function to read layers from the newer .APRX files... I think it's something with the ArcGISProject.listMaps.ListLayers but I'd love some help/advice on howe people resolved this


Here is where I read the layers in from the .APRX and query the ID fields.

    for lyr in, "*", data_frame):
        if ( in list(config_file['layers'].keys())) and (lyr.supports("DEFINITIONQUERY")):
            lyr.definitionQuery = "{} = '{}'".format(config_file['layers'][]['projectIDField'], project_id)

Here is where I validate the .APRX by checking the current layers against the config file.

   # Validate the aprx file
        check_aprx =
        arc_layer_list = [ for layer in]
        config_layers = list(config_file['layers'].keys())

        if set(config_layers).issubset(arc_layer_list):
            logger.log_msg("aprx File = {} --- OK".format(arcpy.GetParameterAsText(0)))
            aprx_valid = True
            logger.log_error("aprx File = {} --- INVALID. aprx File does not contain the correct layers as indicated"
                             " in the config file. Please review.".format(arcpy.GetParameterAsText(0)))

Here is the output/ERROR:

0 Kudos
2 Replies
MVP Emeritus

Adam, I suggest you have a slow read through the new package info for 'mapping' which is not 'mp'

Introduction to—ArcPy | ArcGIS Desktop 

Migrating from arcpy.mapping to ArcGIS Pro—ArcPy | ArcGIS Desktop 

Some things have move, most stayed the same, obvious things like projects are now *.aprx, but all the classes and methods are in the Table of Contents section to the left of the help topic.

eg. Alphabetical list of classes—ArcPy | ArcGIS Desktop 

2 to 3 only helps with the python stuff, there isn't a mapping to mp equivalent, except tracking down the functionality... but the help topics are great and easy to follow

by Anonymous User
Not applicable

Hey Dan,

Sorry for the late reply but your response wasn't helpful at all. I thought it was clear from the screenshots that I was using but regardless, for those experiencing this issue the problem is you need to stipulate each method call separately.

So something like:

for lyr in

Should be:

aprx =
mapView = aprx.listMaps()[0]
for layer in mapView.listLayers():
 if ( in list(config_file['layers'].keys())) and (layer.supports("DEFINITIONQUERY")):
 layer.definitionQuery = "{} = '{}'".format(config_file['layers'][]['projectIDField'], project_id)

Furthermore the main issue actually stemmed from the inability to set map extents (which turns out hasn't been replicated in Do you happen to know when this will be added? It's a bit hard to update code when you don't update the package...

0 Kudos