English

How To: Call a ModelBuilder model in a Python script

Summary

In some situations it is often easier to call a ModelBuilder model in a Python script rather than converting an entire model to a Python script. This technical article describes how to do this.

Procedure

ModelBuilder models are stored in a custom toolbox, and to call them within the Python script the arcpy.AddToolbox() function is required.

  1. Create a variable referencing the toolbox.
    Code:
    tbx = arcpy.AddToolbox("C:/Temp/MyTools.tbx")

  2. Call the model using the variable created in the previous step. In this example 'Model' is the name of the model.
    Code:
    tbx.Model()


    Note:
    The model can also be called up using the alias name for the toolbox (arcpy.toolbox_alias). It is a best practice to add an alias name to the toolbox.

  3. If a model requires a user input, create a variable in the Python script and pass that as the argument to the model parameter. For example, the following code sample passes an input feature class into the copy features tool in the model.
    Code:
    inFC = "C:/Temp/input.shp"

    tbx.Model(inFC)

Related Information