Select to view content in your preferred language

Executing Model through ArcPy

5648
5
04-13-2016 11:51 PM
TinaOosterom1
Deactivated User

Recently I found some info about how one can access models in toolboxes using arcpy.

The command for accessing the toolbox is arcpy.ImportToolbox. But it is not clear to me, how to run the model, which command to use.

E.G. I have a toolbox called ‘UpdateModels.tbx’. In this toolbox I have created a model called ‘FirstStepUpdate’.

The way to access the model would be

arcpy.ImportToolbox('c:/ArcGIS/ UpdateModels.tbx; FirstStepUpdate')

But how do I get arcpy to execute the model?

Tags (2)
0 Kudos
5 Replies
RebeccaWatson
Deactivated User

Tina,

I'm not an expert but I import the toolbox and then run the model processes separately e.g. if I were to use the Create Routes tool from the Linear Referencing toolbox I would do something like:

arcpy.ImportToolbox("C://path/to/Toolbox.tbx")

arcpy.CreateRoutes_lr(pathtoLayertoCreateRoutesfrom, "ROUTE_ID", pathtoCreatedRoutes, "TWO_FIELDS", "START_", "LENGTH", "UPPER_LEFT", "1", "0", "IGNORE", "INDEX")

You might be better exporting your model as python script and take a look at the result in PyScripter or similar  - you can learn a lot like that.

Does that help?

TinaOosterom1
Deactivated User

Hi Rebecca,

Wow, that is quick! Thanks ever so much for responding so promptly.

Your remark about creating Python scripts is correct, but in my case, I have to automate a big bunch of models, that I ‘inherited’ from a colleague that was very productive.

Since he has left already, I struggle most with finding all the input data, for he has been very creative.

So, in order to make some quick progress, it seemed a good idea to, for now, prepare python scripts to execute the models and schedule those. This would buy me time to fully work them out in ArcPy.

Now, in my script I thought this would be the syntax:

0 Kudos
RebeccaWatson
Deactivated User

Hi Tina,

Ok. I'm not sure I fully understand what you need. We run a number of models from python scripts as scheduled tasks each night to update data needed for our web application, but they work like I mentioned above and then the .py file is run via a .cmd file like this:

start c:\python27\arcgis10.1\python.exe "E:\GIS_Scripts\python\Script.py"

I'm guessing I don't have experience of what you need.

I hope you get some answers here.

Rebecca

0 Kudos
TinaOosterom1
Deactivated User

The syntax I made has disappeared in my first answer, so here it is again:

------------------------------------------------------------------

import arcpy

arcpy.env.overwriteOutput = True

arcpy.env.workspace = r"c:/ArcGISdata"

arcpy.ImportToolbox(r"c:/ArcGISdata/Workshop.tbx")

arcpy.Workshopmodel_TestAlias

print "Script finished"

----------------------------------------------------------------

In which Workshop.tbx is the real name of the toolbox, TestAlias is the alias of the toolbox and Workshopmodel is the name of the model I want to run.

0 Kudos
ChadRicks
Regular Contributor

This is my code that works. Hope this helps

import arcpy

import sys, os, smtplib, logging, datetime from arcpy

import env

# Set an gdb admin connection variable.

adminConn = "//MACHINENAME/Scripts/ProdDBO_DefaultSDE.sde"

# Set a few environment variables

arcpy.env.workspace = adminConn

arcpy.env.overwriteOutput = True

print("Running compress")

arcpy.Compress_management(adminConn)

# Script arguments Points to the toolbox where the model is located arcpy.ImportToolbox("\\\\MACHINENAME\\Scripts\\FOMScripts.tbx")

print "Run UpdateAllSpaces Model"

arcpy.UpdateAreaAllSpaces_FOMScripts()

0 Kudos