4

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]
try:    
    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:
    print(arcpy.GetMessages())
    print('\n')
    tb.print_exc(limit=1, file=sys.stdout)

del aprx
1
  • 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
    Aug 20, 2019 at 1:39

1 Answer 1

7

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")
type("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]
m.addLayer("fooName")
# value error  - its a string
m.addLayer(fooResult)
# value error  - its a result object
m.addLayer(fooResult[0])
# [<arcpy._mp.Layer object at 0x000002D941E296D8>]
3
  • 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
    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
    Aug 19, 2019 at 14:12
  • That worked, thanks!
    – recurvata
    Aug 19, 2019 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.