Loop through and run models in a toolbox

251
4
12-08-2023 12:31 PM
sk08
by
New Contributor

Is it possible to loop through all models in a custom toolbox and run them instead of having to list them individually in the script? 

Here is my script. 

import arcpy

# Import custom toolbox
tbx = arcpy.ImportToolbox("E:\HostedFeatureLayers\Stormwater\Stormwater\Default.atbx")

try:
tbx.UpdateswInlet()
print("Finished Append: swInlet")
tbx.UpdateswManhole()
print("Finished Append: swManhole")
tbx.UpdateswDischargePoint()
print("Finished Append: swDischargePoint")
tbx.UpdateswWeirStructure()
print("Finished Append: swWeirStructure")
tbx.UpdateswOpenDrain()
print("Finished Append: swOpenDrain")
tbx.UpdateswPipe()
print("Finished Append: swPipe")
tbx.UpdateswDetention()
print("Finished Append: swDetention")
except arcpy.ExecuteError:
print(arcpy.GetMessages(2))

0 Kudos
4 Replies
DanPatterson
MVP Esteemed Contributor

for line numbers and proper formatting

Code formatting ... the Community Version - Esri Community

Now, what happened when you ran it?

You could also have a look at

ListTools—ArcGIS Pro | Documentation

to see if you can run them that way


... sort of retired...
0 Kudos
sk08
by
New Contributor

The above script worked fine (the formatting didn't come through when I copied it into here).

This is another version I had tried that did not work:

import arcpy 
 
tbx = arcpy.AddToolbox("E:\HostedFeatureLayers\Stormwater\Stormwater\Default.atbx")    

tools = arcpy.ListTools("*_Defaultatbx")

for t in tools:  
  try:
    arcpy.t() 
    print(t) 
  except arcpy.ExecuteError:
    print(arcpy.GetMessages(2))
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
In  [3]:
Line 9:     arcpy.t() 

AttributeError: module 'arcpy' has no attribute 't'
---------------------------------------------------------------------------

 

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Try ListTools—ArcGIS Pro | Documentation ?

Something like this, I think

tbx = arcpy.AddToolbox()
for tool in arcpy.ListTools(tbx):
    tbx.tool()

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

You just can't 'arcpy.t ' it.  Here is a command line example for one of my toolboxes

tbx = arcpy.AddToolbox(r"C:\arcpro_npg\npGeom_32.atbx")
nme = tbx.__name__
nme
'npgtools'

tools = arcpy.ListTools("*_npgtools")
arcpy.Usage(tools[0])
'AttributeSort_npgtools({Tool}, Input_features, Sort_fields;Sort_fields..., {Output_field})'

arcpy.AttributeSort_npgtools( ..... # now what do you see here for your toolset?

... sort of retired...
0 Kudos