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.

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.

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.

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 1abelow. 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 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:

If your model uses an iterator, the iteration logic will not be exported and will have to be replaced with looping statements in Python.

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.

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.

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.

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.

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.

If you expect to be overwriting data, set the overwriteOutput property to True.

arcpy.env.overwriteOutput = True

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.

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.

4 Comments

Hornbyddsays:

This is a useful discussion especially when teaching others about Python and geoprocessing. I often say hey look you can export models straight into Python code from model builder which is a useful starting point in understanding the syntax. So being able to give them a link to say actually it will blow up under certain scenarios, is very helpful.

I think it is perfectly reasonable that some tools don’t work with export to python although efforts should be made to support iterators, in-line variable ect. Exporting to python and setting the script to run on windows task scheduler is a common and regularly practiced workflow. The line below goes to far. It is either fully functional and supported, or it isn’t.

“export models to Python is intended to be used as a learning aid for understanding how tools and environments are accessed in Python.”