0

For a while now we're running a script each night via task scheduler from which several custom models are being run.
In an effort to streamline the code and simplify adding more models in the future I'm attempting to create a list at the start of my script and calling them in a for loop.

import arcpy

Toolbox= r"S:\GEOINFO\05_Tekenkamer\Marc\Test1"
Models = ["TestDissolve","TestTerminals"]

#Import Toolbox
arcpy.ImportToolbox(Toolbox)

#Run models
for Model in Models:
    print ("Executing model " + str(Model))
    try:
       arcpy.TestDissolve_Test1()
       print ("Executing model " + str(Model) + "succeeded")
    except:
       print ("An error occured")

print("End of script")   

The part where I'm stuck is the first line of the try block where I run the model. I'm trying to substitute the fixed part of 'TestDissolve' after arcpy. for the current Model in the list (Test1 is the alias of the toolbox).
The first thing I tried was phrasing it like 'arcpy.Model_Test1' but that gives an error that the model Model can't be found. I don't think you can compose it in a string and then run it, or at least I don't know how.

Another way to fix the issue of running multiple models could be just to run every model from the toolbox somehow but not all of them have to be run every day and some have to be run in a certain order so I didn't try that.

I'm not sure if I'm overlooking something obvious here or if it's even possible but I'm hoping anyone here can help me with this.

Some additional information: we're using ArcGis 10.3.1 (python 2.7) but we might be able to use python 3 for this on our server if that could solve the issue.

How can I call models with arcpy from a list of models?

0

I don't think you can substitute strings for functions in this way, but you could try adding the full function names into the array:

Models = [arcpy.TestDissolve, arcpy.TestTerminals]

then in the try block, you can just call:

Model()
print ("Executing model " + Model.__name__ + "succeeded")
  • Thanks for responding. It took me a bit of trial and error but I got it working the way you suggested. It turned out I had to import the toolbox before I set up the list of models to run otherwise you gte the error "AttributeError: 'module' object has no attribute 'TestDissolve'". Thanks for the help, now I can start implementing it. – Marc Feb 7 '17 at 14:08

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.