Using Python to add multiple datasets to an ArcGIS Pro Project

281
9
01-14-2021 10:19 AM
Katherine_Clark
MVP Regular Contributor

Hello,

I am trying to create a very simple Python script tool for my team to use when opening a new ArcGIS Pro project that will automatically add a pre-determined group of datasets to the map. Below is a code snippet of what I want to do. Some of the data will be shapefiles and some are feature layers within file geodatabases. (the file path in the actual code is the full directory file path, without the ellipses)

 

 

import arcpy
 
aprx = arcpy.mp.ArcGISProject("CURRENT")
aprxMap = aprx.listMaps()[0]

states_shp = r"...\States_NAD1983\states.shp"
aprxMap.addDataFromPath(states_shp)
aprx.save()

 

 

  

I get this error: 

Katherine_Clark_0-1610648084226.png

Apologies for my ignorance here, I'm hoping someone can help me out! Thank you! 🙂

 

-KC

 

Reply
0 Kudos
9 Replies
Kara_Shindle
Occasional Contributor III

I put your code in a blank ArcPro with my own shapefile and it worked fine.  

 

Edited: add some print statements?  Here is a link to someone with the same issue 

Reply
0 Kudos
Katherine_Clark
MVP Regular Contributor

Hmmm, ok. When I reference a shapefile that's stored locally on my desktop, the code worked fine. The dataset I'm referencing is on our company's data server. I'm connected to that network when I'm running the script so I didn't think it would cause issues....is there something else I need to include in the code to make it work?

Reply
0 Kudos
DanPatterson
MVP Frequent Contributor
Katherine_Clark
MVP Regular Contributor

Dan,

I looked into project templates, but was running into various issues regarding loading times, or file paths still breaking, etc. I will continue to look into that as an option though

Reply
0 Kudos
Kara_Shindle
Occasional Contributor III

My shapefile was located on a shared network drive, so I'd verify the file path you are using or try the project template idea.  

Reply
0 Kudos
Katherine_Clark
MVP Regular Contributor

Thanks @Kara_Shindle . I'm wondering, did you use a UNC path or a normal file path in the script?

Reply
0 Kudos
DanPatterson
MVP Frequent Contributor

Are you using unc paths? and how are you denoting them?  I don't use them but I know r"\\ will just be interpreted as an escaped single backslash and not a double backslash, hence the "\\\\ .... one for each \ needed

r'\\\\Somepath
r'\\Somepath


... sort of retired...
Katherine_Clark
MVP Regular Contributor

Good point @DanPatterson , I had forgotten about that when using a raw string! However, unfortunately it didn't seem to make a difference in my script, so it must still be some other underlying issue. 

And just for reference/follow up on this overall thread - I was still having issues packaging the project as a template (either a document or adding to our portal) so I ended up using the "Consolidate Project (Data Management)" tool to create a solution in the meantime. That fixed the issue of broken links happening when the project was copied and pasted elsewhere by creating copies of the data within the project folder itself. While I realize this wouldn't be an ideal solution for datasets that are accessed by multiple people and updated often, this works for us because the datasets are static. 

Reply
0 Kudos
Kara_Shindle
Occasional Contributor III

I was using an UNC path.

Reply
0 Kudos