ArcGIS Pro Tutorial: Creating a Script Tool from a Standalone Python File

Eric van Rees
5 min readSep 12, 2022

In this tutorial, you’ll learn how to create an ArcGIS Pro script tool that is based on a standalone Python file.

ArcGIS Pro screenshot

If you run a geoprocessing tool such as Buffer, you are running a geoprocessing toolbox. ArcGIS Pro enables you to create your own geoprocessing tools that are stored inside these toolboxes. These tools can have different forms, but here we’ll focus on how to create a script tool that is based on a Python script. The idea behind script tools is that you are accessing a tool with a menu that takes user input (such as input/output folder locations), that need to be set manually, or provide multiple options from a dropdown menu.

Besides being more user-friendly than a Python script, a GIS user without Python knowledge will be able to use your Python code after converting it to a script tool. We’ll cover how to do that conversion in this tutorial.

Although the topic of creating script tools from Python scripts is covered in detail in the (excellent) book “Python for ArcGIS Pro”, I’ve found that creating script tools with Pro 3.0 works a little different than what is described there. That book came out before the 3.0 release, so the info from this tutorial, although mostly similar to that book, is a little more up-to-date.

STEP 1: Download the data
This tutorial builds on an earlier tutorial, that describes how to create geodatabase files for a dataset with multiple subfolders and convert existing shapefiles to a geodatabase format. This is done using a Python snippet that is run from a Python notebook. Using Pro, you can create a script tool from a standalone Python file or export an existing Python notebook to a Python file.

In this tutorial, we’ll create a standalone Python file based on a python code example. What this code is not the most important thing here: we need some input code to convert to a script tool so if you have your own script to convert, you can use that too.

First, download the dataset (Natural Earth Quick Start kit) and unzip the file to a convenient location on your hard drive. If you already have it installed from other tutorials, make sure you have a clean, unedited dataset available to see the results in this tutorial. I will use the following file path:

C:\Projects\Natural_Earth_quick_start

Next, make sure you save the following Python snippet (here displayed in a code cell inside a Python notebook) as a standalone Python file (so that it uses the .py extension) on a convenient place on your hard drive. We will have to edit this code a little bit so that it can be used as a script tool (this code comes from another tutorial and can be used as-is):

STEP 2: Edit the code file
The script we’ll create will use this code snippet, but it needs some editing:

· the arcpy package needs to be imported, as script tools do not load it automatically, as is the case with notebooks created inside Pro;

· input/output datasets and file locations need to be set as parameters using arcpy.GetParameterAsText();

· print statements need to be replaced with arcpy.AddMessage() in order to be displayed as custom messages

We’ll do this now. Create a .py file and copy the contents of the python snippet displayed earlier in a code editor of your choice. Next, edit it so that it looks like the image below. You’ll see that it is a little different than before, as the new version adds the arcpy import and the two arcpy functions with input and print statements :

Save the file and close it.

STEP 3: Create the script tool
This is the most complex part of the script tool creation process and it might take you a few times to get it right. This is because you will need to carefully reproduce each step below. If you miss one step, you will have to start over again.

Open ArcGIS Pro and create a new project. In the Catalog pane, click “Toolboxes”, next “new toolbox.atbx” inside and right-click, selecting “New” and “Script”. The window that will open up does not show you an option to add a Script file, as it does on page 231 of the referenced Python book (it says “Toolbox” instead of “Script file”):

First, set the “Name” field to “CreateGDBandConvertSHP” and set the “Label” field to “Create a new file Geodatabase for subfolders and convert existing shapefiles to gdb format”. Next, click the Execution tab and click the folder icon to the far right of the Script File field to browse for an external script. Add the python file (.py) you’ve created under Step 2 and make sure the code gets updated with your own Python code from the referenced Python file.

Next, click “parameters” from the left tab and click “label”, type “workspace”, click the field under “Name”, accept the default and select “Workspace” under Data Type:

Now, the tool understands you want to set a workspace as the single input parameter. Leave the other two presets (“Type” and “Direction”) for this input untouched. Click “OK” and you should see your script tool inside the toolbox.

STEP 4: Run and test your script tool
Double-click the script tool and set the workspace, using the path mentioned under step 1:

After the tool finishes, click “View details” to see all messages (105 in total for this case):

The tool runs correctly and can now be used for any workspace that includes shapefiles.

--

--

Eric van Rees

Writer and editor. Interested in all things geospatial.