AnsweredAssumed Answered

Python 2 to 3 Upgrade - Help with listLayers

Question asked by dird_arice on Jun 10, 2019
Latest reply on Jun 30, 2019 by dird_arice

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

 

CODE:

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

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

 

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

   # Validate the aprx file
    try:
        check_aprx = arcpy.mp.ArcGISProject(arcpy.GetParameterAsText(0))
        arc_layer_list = [layer.name for layer in arcpy.mp.ListLayers(check_aprx)]
        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
        else:
            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:

Outcomes