In my current role, scripting is by far my favourite activity. I’ve always enjoyed language, and in the same way I enjoy writing and reading other’s scripts. Although reading, understanding, and altering scripts written by others can often be seen as an area where the drudgery must be mitigated or avoided, I enjoy trying to understand the thinking behind the code and learning bits and pieces from other authors along the way.
One of the more challenging alterations, however, is the ModelBuilder script export. For a number of reasons – scheduling, making a base for a script, integrating a process to fit into others etc. – having a process as a Python script may make life easier. Without editing, though, a Python script that has been exported from a model can be difficult to read and understand.
There is often a need to return to a script to understand its function, alter it based on new requirements, or add functions that are easier to set up or only possible in Python. For this reason, it is a good idea to alter the exported script so that it is more readable for anyone who needs to understand or alter it and they don’t have to spend hours puzzling out exactly how it works and how to alter it. Altering a script can be a way to start getting to grips with scripting with Python and arcpy for complete beginners.
This blog describes some ways to tidy up the script for easier reading and alteration for exports from models that don’t use user input parameters. To export a model to a Python script, use this ESRI guide.
This blog uses the following terms:
Accessing the exported script
To access the script, open it in a Python IDE. If you have ArcGIS installed, you will be able to edit it with IDLE by right clicking the file and selecting Edit with IDLE.
Note: This blog assumes that Python version 2.7 is installed. If using ArcGIS Pro, you may have version 3+ installed and some of the syntax may have changed. See here for details of main differences and conversion between versions.
I have broken the exported script into four sections and will describe them individually.
The header will look something like this:
The header should contain useful information about the script, for example, script version, author, date created or modified, and purpose. If you have entered a model description for the model that was exported, a description will already be populated. How you format the header is up to you. Each line should be preceded by a ‘#’ so it is interpreted as a comment and not as code.
This section has a list of the modules that will be imported. Scripts that have been exported from models will and should always contain the line:
arcpy is the Python module that contains Esri functions. If the function and purpose of the script is expanded, other modules may be added here. Each new module should be entered on a new line, for example to add the os module as well as arcpy:
This section is where it is important to tidy up the variables and where the most potential confusion creeps in for readers of the script.
You may have given helpful and descriptive names to the elements before exporting to a script. Those names and descriptions become the variable names when you export to a Python script and have a tendency to become long and unwieldy as in the case of the last two variables in this list:
Descriptive information should be transferred from the variable name to a script comment. Variable names should be kept short, represent what the variable value is, and be easily understandable where possible.
You may also encounter more than one variable with the same value. In this example, three variables all have the value “Quads_Layer.” There is no need for this repetition, there should just be one variable with this value. This is one way of renaming the variables and reformatting the line spacing from the example above so it is easier to read:
Notice that the variables now have descriptive comments.
Everywhere the old variable name appears it will have to be changed to the new variable name. There is no getting away from the fact that this can be fiddly work. The pain can be eased by using the IDE find/replace tool or equivalent. In IDLE, it can be found by going to the Edit menu and selecting Replace…
Some lines can be tidied up by removing superfluous information that describes default settings. They are often difficult to read and add little value. If preferred, you can use a comment to tell the reader that default settings have been used. Here is one line in the model export script:
The long line of text simply makes a layer using the tool Make Feature Layer (Data Management) and sets all fields to visible which is the default for this tool.
In ArcGIS documentation, each tool has a description of the input parameters. If they are described as optional, they do not need to be included in the script unless they are different from the default. As we can see Make Feature Layer only has two mandatory variables. If all the other settings are the default settings, we can tidy up the line to this:
By tidying up a Python script that you have exported from ModelBuilder, you can get a better feel for scripting with Python and arcpy and save hours of work for yourself and/or your colleagues.
Posted by Tanya Knowles, GIS Technician, Exprodat.