Exporting a model to a Python script
The option to export a model to a Python script is valuable in helping you learn how tools and environments are used in Python, and often can jumpstart your development. After editing and completing the script, it can be added to a toolbox or model as a script tool and run as any other geoprocessing tool. To export a model to a script, follow the steps below:
![Legacy Legacy](rsrc/legacy.png)
Functionality to export models to JScript and VBScript has been removed from ModelBuilder at version 10.
Steps:
- From the menu in ModelBuilder, point to and click Model > Export > To Python Script.
- Click the Save in drop-down arrow and navigate to the location where you want to save your script.
- Type a file name for the script.
- Click Save.
Editing an exported Python script
There are instances when your exported model will not work. Take the following points under consideration when exporting a model to a script:
- If you expect to be overwriting data, set the arcpy.env.overwriteOutput property to True.
- If your model used layers or table views that weren't created within the original model, those layers or table views will have to be created in the script using tools such as Make Feature Layer and Make Table View.
- Data elements from your model are converted directly into variables in the exported script. Avoid naming data elements that will be incompatible in Python (for example, class, global, and return). For a full list of reserved keywords, use Python's keyword module.
import keyword print keyword.kwlist
- If your model used any inline variable substitution, such as %workspace%, %scratchworkspace%, %n%, %i%, or %variable name%, those will have to be substituted with the correct value.
- If your model used an iterator, the iteration logic will not be exported and will have to be replaced with the looping statements in Python.
- If your model used a feature set or record set, the following code will be created in the export.The code attempts to guard against null or invalid input by setting the Feature_Set variable to an in_memory feature class. Since the in_memory feature class will not exist at the time of execution, you need to update the path to point to a feature class which is known to exist during execution. You could also change the logic to throw an error message and stop the execution if that makes more sense for your workflow.
Export from Model with a feature set parameter
Feature_Set = arcpy.GetParameterAsText(0) if Feature_Set == '#' or not Feature_Set: Feature_Set = "in_memory\\{F15C6411-0F4F-4A68-9BAC-29D6E7874669}" # provide a default value if unspecified
- If your model used a Model only tool, such as Merge Branch, Collect Values, or Calculate Value, these tools will not run in Python. It is required to implement the equivalent Python functionality these tools provide. For example, you will need to use if/else logic to do branching in your script.
- If your model included submodels, the contents within those submodels will not be exported. Instead, the toolbox will have to be referenced and the model tool will need to be called inside the script.
10/29/2012