Select to view content in your preferred language

Define ArcGISProject() by "Current" vs file path

180
1
12-18-2024 07:13 AM
AaronGlanville
Occasional Contributor

Hello,

I have a script to pull editor tracking information from a list of feature services to upload a .csv to SharePoint. I run this script directly through Notebooks in ArcGIS Pro (3.3). However, when I tried to run an adjusted script as a python file (and also run that .py through a .bat) I encountered errors.

I think I have narrowed my issue down the most I could on my own, but I'm now stuck. I believe my errors originated when changing how I connect to my Pro project. 

When I changed the script from project = arcpy.mp.ArcGISProject("CURRENT") to project = arcpy.mp.ArcGISProject(r"project_path")  to account for running the script outside the project, it broke the script in a couple ways.

First, when using the listTables() function to find what tables are in the map, depending on if I define the project by CURRENT vs the file path, I get different results. When defining a project using the file path, it lists tables that are not show in the map's Contents Pane. Those tables are the .csv I upload and another copy of that data saved in a local gdb with the date in the file name.

Second, when defining the project by file path, the removeTable() function no longer removes the tables. Both in the sense that it will not remove tables from the Content Pane if they are there, as well as remove tables from whatever source the listTables() is pulling information from when the tables are not shown in the Contents Pane.

I would greatly appreciate anyone's thoughts on what I am missing here, I am a novice when it comes to arcpy.

Below is a testing script I used to help identify my errors for reference

import arcpy
import time
from datetime import datetime

# Set the current project and map
#project = arcpy.mp.ArcGISProject("CURRENT") #I'll switch between commenting out this line and the next for testing purposes
project = arcpy.mp.ArcGISProject(r"project_path")
print("Project loaded: ", project.filePath)
map = project.listMaps("Edit Tracking")[0]
print("Map loaded: ", map.name)

print("\nAll tables:")
for table in map.listTables():
print(f"{table.name}")

print("\nAttempting to remove tables")
for table in map.listTables():
print(f"Checking table: {table.name}") 
if table.name == "Asset Edit Tracking.csv" or "CombinedDataTable_" in table.name:
table_name = table.name # Store the table name before removal
map.removeTable(table)
print(f"Removed table: {table_name}") 

print("\nAll tables again:")
for table in map.listTables():
print(f"{table.name}")

 

0 Kudos
1 Reply
BrennanSmith1
Regular Contributor

When sharing code samples, use the </> button for "Insert/Edit Code Sample" (you might need to click the three dots to open all the formatting options), this makes it a lot easier to read. Also, I don't know if it breaks anything, but I generally avoid using "map" as a variable name, use "m" instead since it's not a built in function.

When changing an aprx from python, you'll need to end the script with aprx.save(), or project.save() in your case. That would explain why removeTable() isn't working as expected. I'm not sure why you're seeing additional tables that aren't in the Map's TOC though.

 

0 Kudos