I am fairly new to python and need recommendations on how to handle internal/helper function failures.

I have a script that that imports a list of jurisdictions, then iterates through that list and creates a view, map, and experience builder application tailored to that jurisdiction. It's working more or less flawlessly but is routinely failing on the third iteration. And the failure happens due to an error with the arcgis\features\managers.py helper.

KeyError                                  Traceback (most recent call last)
c:\Users\user\path_to_project\script.py in <cell line: 40>()
     224 for j in jList: #changed 'jurisdiction' to 'j'
     225     #*Create Views
---> 226     jView = masterLayer.manager.create_view(name = j + '_view',      spatial_reference = sr, tags = tagsText, capabilities = "Create,Query,Update,Delete,Editing,ChangeTracking")
     227     jView.move(folder = folder)
     228     jViewFLC = arcgis.features.FeatureLayerCollection.fromitem(jView)

c:\Users\user\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\lib\site-packages\arcgis\features\managers.py in create_view(self, name, spatial_reference, extent, allow_schema_changes, updateable, capabilities, view_layers, view_tables, description, tags, snippet, overwrite, set_item_id, preserve_layer_ids)
   2006             params["description"] = description
   2007         res = gis._con.post(path=url, postdata=params)
-> 2008         view = content.get(res["itemId"])
   2009         fs_view = FeatureLayerCollection(url=view.url, gis=gis)
   2010         add_def = {"layers": [], "tables": []}

KeyError: 'itemId'

The loop runs all the way through the first two items in the list and then fails routinely on the third. It was running all the way through two days ago and nothing has changed with this aspect of the script.

How do I isolate the error when it happens within the package?

1 Answer 1


This Python error handler (with traceback) for arcpy is a useful piece of code to include with all your scripts. It might provide you with more information on troubleshooting your problem. See here for more help with error handlers in ArcGIS Pro. Here is some basic information on KeyErrors.

import arcpy
import sys
import traceback


    Your indented code goes here


except arcpy.ExecuteError: 
    # Get the tool error messages 
    msgs = arcpy.GetMessages(2) 

    # Return tool error messages for use with a script tool 

    # Print tool error messages for use in Python

    # Get the traceback object
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]

    # Concatenate information together concerning the error into a message string
    pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
    msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n"

    # Return Python error messages for use in script tool or Python window

    # Print Python error messages for use in Python / Python window

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.