2

I'm trying to write a script for a tool in ArcGIS Pro 2.x for an annual workflow where I create five time-peroid directories (named 1-5) and then I create both a new file geodatabase and an ArcGIS Pro project (using the same name as the folder). I then need to set this geodatabase as the default geodatabase.

My script runs to completion and the ArcPy messages state the default to be the newly created one. However, when I open up each new project in Pro, the newly created geodatabase is not set as the default (nor is it even connected).

# 0. Set-up
import arcpy, os, sys
arcpy.env.overwriteOutput = False

# Read in tool string parameter, year
year = arcpy.GetParameterAsText(0)

# Get the pathname to this script 
script_directory = sys.path[0]

# Instantiate ArcGISProject object and assign variable aprx to it
arc_project = os.path.join(script_directory, 'Classification_Project_Template.aprx')
aprx = arcpy.mp.ArcGISProject(arc_project)

# 1. Create new folder for year
new_year_directory = os.path.join(script_directory, year)
if not os.path.exists(new_year_directory):
    os.makedirs(new_year_directory)

# Create 5 time-period directories (named 1 through 5) > within each, make a copy of .aprx, create new geodatabase, and set it as the default 
for t in range (1, 6):
    time_directory = os.path.join(new_year_directory, str(t))  
    os.makedirs(time_directory)
    aprx_copy_name = str(t) + '.aprx'
    aprx_copy_path = os.path.join(time_directory, aprx_copy_name)
    aprx_copy = aprx.saveACopy(aprx_copy_path)
    # Not sure if aprx_copy will suffice as new project object, but created new one, aprx_new, as ESRI documentation states: "The method [saveACopy] creates a new output project file, but the project variable continues to reference the original ArcGISProject object."
    aprx_new = arcpy.mp.ArcGISProject(aprx_copy_path)  
    # Create file geodatabase for each time-period directory 
    geodatabase_name = str(t) + '.gdb'
    geodatabase_path = os.path.join(time_directory, geodatabase_name)
    arcpy.AddMessage('Desired default geodatabase_path is: ' + geodatabase_path)
    arcpy.CreateFileGDB_management(out_folder_path = time_directory, out_name = geodatabase_name)
    aprx_new.defaultGeodatabase = geodatabase_path
    arcpy.AddMessage('Default geodatabase is set to: ' + aprx_new.defaultGeodatabase)
  • Try calling aprx_new.Save() right after aprx_new.defaultGeodatabase = geodatabase_path? – Dowlers Jun 12 at 17:48
  • @Dowlers bless you, that worked :) just with lower case s as in aprx_new.save() If you can add this as an answer, I'll make sure to accept it – Iracambi Jun 12 at 17:56
  • Glad you got it working. – Dowlers Jun 12 at 18:00
3

The code needs to save the updated geodatabase path. Add aprx_new.save() right after aprx_new.defaultGeodatabase = geodatabase_path

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.