0

I wrote some simple code that created multiple polygons using the Buffer tool. I will use it in a toolbox and therefore want the outFeatureClass to be automatically added to the current map.

The following code doesn't work. "outcome" is the outFeatureClass I want to add.

pw = r"C:\Users\Main\GIS\Data\ArcGISProject\ArcGISProject.gdb\outcome"
aprx = arcpy.mp.ArcGISProject("CURRENT")
aprxMap = aprx.listMaps("Map")[0]
aprxMap.addLayer(pw)

Running the code in the ArcGIS Pro Python command line I receive an error:

 "ValueError: r"C:\Users\Main\GIS\Data\ArcGISProject\ArcGISProject.gdb\outcome"

The env.workspace for the Python file is set to r"C:\Users\Main\GIS\Data\ArcGISProject\ArcGISProject.gdb".

1
  • What is the full error message including line number? If you have set arcpy.env.workspace then that should be part of the code snippet that you present. – PolyGeo Sep 4 '20 at 19:11
1

If you read the help file on the addLayer() method for a map it takes a "Layer or LayerFile object". You have passed in pw which is a string which happens to represent a path to a Feature Class, so not a Layer Object. ArcPro wants you to access a dataset via a layer file but as you are directly accessing the FeatureClass I assume a lyrx file does not exist.

You create a Layer object using the MakeFeatureLayer tool.

So this is what you can do in ArcPro 2.6:

with arcpy.EnvManager(addOutputsToMap=True):
  pw = r"C:\Users\Main\GIS\Data\ArcGISProject\ArcGISProject.gdb\outcome"
  arcpy.MakeFeatureLayer_management(pw,"Outcome")
1
  • Thanks. This works fine when running the code in the ArcGIS Pro Python command line. When I run it in a toolbox it doesn't work although there is no error message. – svd Sep 5 '20 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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