In a script, I'm creating a feature layer and trying to add it to a map in ArcGIS Pro 2.3.0. When I try to add it though, I get a ValueError. Any ideas why and how to fix it? m is the map object. The layer is created.

FWIW, I'm running the script in Spyder, Python 3.6.6, which I installed (maybe correctly, maybe not) in the default ArcGIS Pro environment. It seems to recognize the arcpy module, although there's no intellisense.

aprx = arcpy.mp.ArcGISProject(prj)
m = aprx.listMaps()[0]
target = m.listLayers('Cities Villages Twps')[0]
    target.definitionQuery = "NAME = 'Chicago'"
    arcpy.CopyFeatures_management(target, 'GPP')
    arcpy.MakeFeatureLayer_management('GPP', "Area501")
    print(type("Area501"))   # str
    print(type(Area501))     # NameError: name 'Area501' is not defined
    print(type(Area501[0]))  # NameError: name 'Area501' is not defined
    m.addLayer(Area501[0])   # NameError: name 'Area501' is not defined   

except Exception:
    tb.print_exc(limit=1, file=sys.stdout)

del aprx
  • 1 - Please don't just say "I get a ValueError", when you get an error, always include the entire exception traceback as text (formatted as code {}) in your question. 2 - Never ever use try: except: statements when posting questions here, you won't be able to see the entire exception traceback.
    – user2856
    Commented Aug 20, 2019 at 1:39

1 Answer 1


It's failing because you're passing in a string to the addLayer function and hoping the method will resolve to the layer you created. In short, it wont. You'll need to be more explicit in what you pass addLayer - the actual layer from the result. In the below code I know that grabbing the first index ([0]) gets the layer object.

fooResult = arcpy.MakeFeatureLayer_management('Points', "fooName")
# <class 'str'>
type(fooResult )
# <class 'arcpy.arcobjects.arcobjects.Result'>
type(fooResult [0])
# <class 'arcpy._mp.Layer'>

p = arcpy.mp.ArcGISProject("current")
m = p.listMaps()[0]
# value error  - its a string
# value error  - its a result object
# [<arcpy._mp.Layer object at 0x000002D941E296D8>]
  • Thanks, but when I leave off the quotes I get 'NameError: name 'Area501' is not defined'. Added some more code in case the problem is there.
    – recurvata
    Commented Aug 19, 2019 at 14:11
  • You need to assign a variable to arcpy.MakeFeatureLayer_management('GPP', "Area501"), the very first line of code -- I used the variable foo and the string foo (bad form on my part). I just updated to make it a bit more clear in the differences.
    – KHibma
    Commented Aug 19, 2019 at 14:12
  • That worked, thanks!
    – recurvata
    Commented Aug 19, 2019 at 14:28

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.