ArcGIS Pro Python Add Layer Not Working

662
5
Jump to solution
12-01-2021 06:14 PM
StrotherSimpson
New Contributor II

Hello!

 

I am using PYCHARM trying to do something I think is simple but I cannot get it to work!

Inside of PYCHARM I would like to create a new feature class then add it to the map. My code creates the feature class and I can see it in the Geodatabase but it will not show up on the map.

I have tried: addLayer(), addDataFromPath()

import arcpy

path_workspace = r"C:\Users\Strot\Desktop\ArcGIS Default Map\ArcGIS Default.gdb"
arcpy.env.workspace = path_workspace
arcpy.env.overwriteOutput = True
arcpy.env.addOutputsToMap = True

string_shape_name = "New_Layer_Shape"

aprx = arcpy.mp.ArcGISProject(r"C:\Users\Strot\Documents\ArcGIS\Projects\Test\Test.aprx")
mp = aprx.listMaps("Map")[0]
fc = arcpy.CreateFeatureclass_management(r"C:\Users\Strot\Documents\ArcGIS\Projects\Test\Test.gdb", string_shape_name, "POLYGON")[0]
fl = arcpy.MakeFeatureLayer_management(fc, string_shape_name)[0]
mp.addLayer(fl)

I have called Pro support and done all the searching I can, please help!

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

Did you save the project after?  The results won't be present if you have the project open while you are doing this unless you are running a notebook or using Pro's python window.  

aprx.save()


... sort of retired...

View solution in original post

5 Replies
DanPatterson
MVP Esteemed Contributor

Did you save the project after?  The results won't be present if you have the project open while you are doing this unless you are running a notebook or using Pro's python window.  

aprx.save()


... sort of retired...
StrotherSimpson
New Contributor II

Wow I think you are right! I checked the map layers using list layers before and after and its not there before but is after.

So I tried the save and got this error, do you have any idea what this could be from?

Thank you so much for your help!

#Command
aprx.save()

# Error
Traceback (most recent call last):
File "C:/Users/Strot/OneDrive - Desert Royalty Company/Side Projects/5 - Python/2021.11.30 - ArcGIS Operator Development Pace/new_arcgis.py", line 23, in <module>
aprx.save()
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 285, in save
return convertArcObjectToPythonObject(self._arc_object.save(*gp_fixargs((), True)))
OSError: C:\Users\Strot\Documents\ArcGIS\Projects\Test\Test.aprx

 

0 Kudos
MichaelVolz
Esteemed Contributor

Are you using One Drive Cloud storage for this?  If so can you try using a local or network drive instead just to test if those locations will work?

StrotherSimpson
New Contributor II

I tried moving it off cloud to local and still got same error.

But it did work if I closed the project, so maybe there is some issue with it being open and trying to save.

Anyways so happy its working, I can live with it being closed that is probably how I intend to run the script anyways. 

Please let me know if anyone has any ideas to this OS error, but so thankful to you guys for the help on this!

0 Kudos
DanPatterson
MVP Esteemed Contributor

where ever you are saving... no spaces, no punctuation in the paths

Save local

Copy to the "cloud" for backup

works every time 😉


... sort of retired...
0 Kudos