ArcMap

Considerations when exporting a model to a Python script

Since the release of ArcGIS 9, you have been able to export models created in ModelBuilder to a Python script. While this option is valuable in helping you see how tools and environments are used in a scripting environment, there are many instances when your exported model will not work as expected.

This article explains why the option to export models to Python is intended to be used as a learning aid for understanding how tools and environments are accessed in Python. As models become more sophisticated to include techniques such as iteration or in-line variable substitution, this export option becomes less useful.

In recent releases of ArcGIS, new ways for understanding how geoprocessing tools and environments are used in Python are provided and documented.

  1. Tool Documentation – The help for every geoprocessing tool includes one or more Python samples, and many of those samples include Python scripting techniques such as using loops, if/else logic, and exception handling.
  2. Copy as Python snippet – Any time you execute a geoprocessing tool in ArcGIS, you can go to the Results window, right click on the result and Copy as a Python snippet. You can paste this snippet in the Python window or any Python script.
  3. Python window – All Python functionality is exposed through the Python window. It is a fully interactive window and a useful location to learn Python and experiment with code. Skills learned here can be applied to larger, more sophisticated scripts. Tools or functions that already have been executed, can be recalled, edited and re-executed. The content can also be saved to a new script.

The Export > To Python Script option will remain a part of ModelBuilder. However, only simple models will work when exported.

Let’s first look at a simple model shown in figure 1a below. This model is comprised only of built-in geoprocessing tools and the workspace environment is set in the model properties. Exporting the model to Python creates a working script as shown in figure 1b.

Figure 1a: Simple model

Figure 1b: Simple model exported to Python

The model shown in figure 2a below will not work when exported to Python. This model includes an iterator (Iterate Multivalue), which executes the model for each dataset connecting to it, and uses variable substitution. The exported script in figure 2b does not include any iteration logic and will not work as expected when executed. In this case, you need to add the correct looping statements and modify the local variables.

Figure 2a: A model that uses iteration and variable substitution.

Figure 2b: Results from a model exported containing iteration and variable substitution.

Figure 2c shows the updated Python script where the local variables are updated and the iteration logic is added.

Figure 2c: Updated script

Here is a list of things to consider before exporting models to Python:

  1. If your model uses an iterator, the iteration logic will not be exported and will have to be replaced with looping statements in Python.
  2. If your model used a Model only tool such as Merge Branch, Collect Values, or Calculate Value, these tools will not run properly in Python; they may execute without error, but nothing happens and your script doesn’t work as expected. In some cases, the Python script will fail at a later point. You need to implement the equivalent Python functionality these Model only tools provide.
  3. If your model used inline variable substitution such as %workspace%, %scratchworkspace%, %n%, %i%, or %variable name%, you will have to replace these with the correct value in the Python script.
  4. If your model used layers or table views from ArcMap or ArcGlobe, those layers or table views will have to be created or the data will need to be fully referenced in the script.
  5. If your model used layers or table views that are not created within the original model, those layers or table views will have to be created in the script using tools such as MakeFeatureLayer and MakeTableView.
  6. If your model included sub-models, the contents within those sub-models will not be exported. Instead, the toolbox will have to be referenced and the model tool will need to be called inside the script.
  7. If you expect to be overwriting data, set the overwriteOutput property to True.

    arcpy.env.overwriteOutput = True

  8. Variables in your model are converted directly into Python variables in the exported script. You need to avoid naming your model variables the same as Python’s reserved keywords (for example, class, global, return). For a full list of reserved keywords, use Python’s keyword module, illustrated below.
  9. If your model has an input variable type that is a FeatureSet or RecordSet, it will be exported as an in-memory feature class such as “in_memory\{E83BC42B-3578-4D20-A817-5F2AF3F5D05E}”. You need to update this variable in Python with a path to a valid dataset or create a script tool where the input data type is a feature set.

Call the model from inside the script

If you want to use a model in Python, you can easily access the model tool inside the script rather than exporting it or re-writing the same logic. For example, custom tools created in ModelBuilder and stored in a custom toolbox can be accessed in a Python script like any system tool by importing the custom toolbox.

import arcpy 
arcpy.ImportToolbox(“c:/mytools/geometrytools.tbx”, “mytools”) 
arcpy.CreateRegularPolygons_mytools(parameter1, parameter2)

Useful Links

Learning Python
Exporting a Model
Using tools in Python
Adding toolboxes in Python
Listing data
Using Python in ArcGIS Desktop 10 – Web Course

 

Next Article

Version 430.1 of ArcGIS for AutoCAD (May 2024) adds support for AutoCAD 2025

Read this article