Frequently asked question

What are the advantages of calling a model directly from a Python script instead of exporting the model to a Python script?

Last Published: April 25, 2020


Python scripts exported from ModelBuilder models may not work because complex parameters or tools available in ModelBuilder are unavailable in Python. For more information about exporting a Python script from ModelBuilder, refer to the following web help document, ArcGIS Desktop: Exporting a model to a Python script.

There are several reasons calling a model from a Python script is recommended instead of calling a Python script exported from ModelBuilder:
  • Some ModelBuilder functions, such as iterators and branching, are unavailable in Python. For example, if a script exported from ModelBuilder contains iterators, the script must be edited to replace the iterators with 'loop' statements. This may result in several syntax errors if edits are not thoroughly done.
  • Every time an update is performed on a model, the script must be re-exported for the changes to take place. Calling a model from Python automatically updates every edit performed.
  • Calling a model from a Python script produces a shorter script, which reduces the risk of programming errors.
There are three ways to execute a Python script in ArcMap:
  • Run as a standalone script by executing the .py file.
  • Use the Python window in ArcMap. To do so, go to Geoprocessing on the ArcMap main menu, and select Python. For more information, refer to the following web help document, ArcGIS Desktop: What is the Python window?
  • Add the script as a tool in ArcMap. For more information, refer to the following web help document, ArcGIS Desktop: Adding a script tool.
The following is a code sample to create a Python script that calls a working model.
def createScript():
    import os
    tbx_path = raw_input("Full path to toolbox: ")
    tbx_alias = raw_input("What is the alias of your toolbox?\n (can be found in the tbx properties) ")
    modelname = raw_input("What is the model name?\n (hint: this is not necessarily the same\n as the label and can be found in the model properties) ")
    outputLocation = raw_input("Where do you want the script located? ")

    outScript = open(os.path.join(outputLocation, ""), "w")

    outScript.write("import arcpy\n")
    outScript.write('arcpy.ImportToolbox(r"' + tbx_path + '")\n')
    outScript.write('print "toolbox imported successfully"\n')
    outScript.write("arcpy." + modelname + "_" + tbx_alias + "()\n")
    outScript.write('print "model completed successfully"\n')

if __name__ == '__main__':

Article ID:000013005

  • ArcMap

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options