How To: Call a ModelBuilder model in a Python script


In some scenarios it is often easier to call a model within ModelBuilder using a Python script rather than converting the entire model to a Python script. Before calling the model, a custom toolbox must be added to the Python script as models are stored in a custom toolbox. For more information, refer to ArcGIS Desktop: Adding and connecting data and tools.


To call a model in a Python script, use the arcpy.AddToolbox() function to add the custom toolbox. Then call the model by either referencing the toolbox in a parameter, or call the model directly using the alias of the toolbox. The following steps describe how to do so: 

  1. Create a variable referencing the toolbox. The following is an example of using the arcpy.AddToolbox() function:
    tbx = arcpy.AddToolbox("C:/Temp/MyToolbox.tbx")
  2. Call the model using either one of the following code samples.
    • To call the model using the defined variable
      MyToolbox is the custom toolbox added in step 1. The model labelled Sample1 is located in MyToolbox. Use the following code sample:
    • To call the model using the predefined toolbox alias
      SampleMyTools is the predefined toolbox alias of MyToolbox added in step 1.
      It is a best practice to add an alias name to the toolbox. The alias cannot contain spaces or special characters, such as an underscore (_).

If a model requires user input, create a variable in the Python script and refer the input shapefile location as the argument to the model parameter. For example, the following code sample passes the input feature class into the copy features tool within the model.

inFC = "C:/Temp/input.shp"


Related Information