Run Model Builder models thru Python.

6223
12
06-25-2015 08:35 AM
lelaharrington
New Contributor III

i have 4 models that i created i am trying to run them thru python to get them to execute one at a time. i have two ways i want to try to do this. a. have python run the models ( to no success as of yet)

b. export each model out as a script and have a master script that calls on 4 scripts to run one after the other until all 4 have ran in sequence.

i am very new to this and would love any help.

here is what i have so far. and here are my errors

ERROR

Traceback (most recent call last):

  File "F:\GIS\Python_Scripts\GLE_BIS_Silverlight_Overwrite\Archive\Delete_Temp_Holding.py", line 16, in <module>

    arcpy.ImportToolbox("F:/GIS/Python_Scripts/GLE_BIS_Silverlight_Overwrite/GLE_BIS.tbx")

  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\__init__.py", line 90, in ImportToolbox

    return import_toolbox(input_file, module_name)

  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\toolbox_code.py", line 441, in import_toolbox

    mymodule = generate_toolbox_module(toolbox, None, False, False, False, module_name, use_alt_alias)

  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\toolbox_code.py", line 416, in generate_toolbox_module

    'exec')

  File "F:\GIS\Python_Scripts\GLE_BIS_Silverlight_Overwrite\GLE_BIS.tbx", line 63

    def SHP-BIS():

           ^

SyntaxError: invalid syntax

Script

import arcpy, time, os, sys

Date = time.strftime("%m-%d-%Y", time.localtime())

Time = time.strftime ("%I:%M:%S :p",time.localtime())

LogFile = file(r"F:\GIS\Python_Scripts\Logs\GLE to BIS PROJECTION AND TRANSFER_" + Date + ".txt", 'w')

output = open(r"F:\GIS\Python_Scripts\Logs\GLE to BIS PROJECTION AND TRANSFER_" + Date + ".txt", 'w')

arcpy.ImportToolbox("F:/GIS/Python_Scripts/GLE_BIS_Silverlight_Overwrite/GLE_BIS.tbx")

# Local variables:

output.write("Script Started At: " + str(Date) + " " + str(Time) + "." + "\n" + "Start Delete Process" +"\n")

# Process: Delete Temp Holding Shapefiles

arcpy.DeleteTempHolding_GLE_BIS()

try:

     # Process: Delete Temp Holding Shapefiles

        if arcpy.DeleteTempHolding_GLE_BIS():

            output.write("Delete of Temporary files SUCCESS","\n")

except:

    output.write("Delete of Temporary files FAIL","\n")

output.write("END OF SCRIPT","\n")

# reset date and time from when  started

lDate = time.strftime("%m-%d-%Y", time.localtime())

lTime = time.strftime("%I:%M:%S %p",time.localtime())

# write to log and close

output.write(str("GLE to BIS Script Ended at  " + str(lDate) + " " + str(lTime) + "." + "\n"))

output.close()

now = time.time()

print "Script Complete"

Tags (2)
0 Kudos
12 Replies
BlakeTerhune
MVP Regular Contributor

According to the documentation​ for ImportToolbox():

If the toolbox does not have an alias, the module_name is required.

Does your toolbox have an alias?

lelaharrington
New Contributor III

yes

Thank you

Lela Harrington

lelaharrington
New Contributor III

problem is not solved at all yet. 

Thank you

Lela Harrington

AlexanderNohe1
Occasional Contributor III

I would wipe out the underscores from your model and then provide a second argument to your import toolbox statement and then use that when calling the tool.

That is:

Change DeleteTempHolding_GLE_BIS to DeleteTempHoldingGLEBIS

then

arcpy.ImportToolbox("F:/GIS/Python_Scripts/GLE_BIS_Silverlight_Overwrite/GLE_BIS.tbx", "myTbx")

Then call your tool:

arcpy.DeleteTempHoldingGLEBIS_myTbx()

0 Kudos
lelaharrington
New Contributor III

ill give it a try and let you know . thanks 

Thank you

Lela Harrington

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Lela,

<<deleted some bad information>>

To help others with viewing your code for debugging, check out Posting Code blocks in the new GeoNet       Spacing is critical in Python code, and Geonet has a way of altering the spacing which makes it hard to debug.

By the way, I personally, I like your second approach (b.) to create separate scripts (instead of leaving them i model builder), but that is just because I feel you can have more control when scripting.

I hope this helps.

Edit:  eliminated bad(thanks for catching that Dan)

0 Kudos
lelaharrington
New Contributor III

if i create the additional scripts instead of calling the model how can i call multi scripts into one script? 

Thank you

Lela Harrington

0 Kudos
JeffWard
Occasional Contributor III

Lela,

You can export each model as a python script, then you can copy all four scripts into one .py file enclosed in function definitions, then run each function in turn at the bottom of that file.

def func1():
  do stuff

def func2():
  do more stuff

def func3():
  and yet more

def func4():
  last stuff

func1()
func2()
func3()
func4()
Jeff Ward
Summit County, Utah
RebeccaStrauch__GISP
MVP Emeritus

Lela, in my workflow I'm usually running one script (from either a custom Toolbox or a Python addin I created), because I typically want to look at the result before I run the next.  However, you can stack up scripts, models, tools, etc and the will process in order, however, if there is an error in the first, it may not stop the second from running..

But there are many threads out there on the subject, and a quick search came up with arcgis 10.0 - ArcPy and running Python scripts (with parameters) within another Python script - Geog...

It's 10.0, but I'm sure its about the same for any 10.0+.  When looking at some of the comments on that page, it referenced a broken link to a blog from Jason Pardy​ which lead me to search the blog, and although I didn't find the one references, I did find another Considerations when exporting a model to a Python script | ArcGIS Blog which looks like something you should look at.

I'm also interested in finding an answer to your question....not that I need it right now, but always good to know.  I use Geonet to research many of my questions....but doing a straight browser search can give you many leads too.  But there may be others here that have better ways.  Many tools in the toolbox! (thankfully!)

EDTI: looks like Jeff already provided an answer.