Python and geoprocessing

Python is the scripting language of ArcGIS. ArcGIS includes a Python API, ArcPy, that provides access to all geoprocessing tools as well as additional functions and classes, and specialized modules that help you automate GIS tasks. You can write scripts that use ArcPy in conjunction with a diverse set of functionality provided through Python standard and third-party libraries. You can run Python commands and scripts with ArcGIS Notebooks, in the Python window, through script tools, or you can run Python outside of ArcGIS Pro. However you run Python, commands work fundamentally the same way and use the same geoprocessing tools.

Below is a simple geoprocessing script that uses ArcPy.

import arcpy

roads = "c:/base/data.gdb/roads"
output = "c:/base/data.gdb/roads_Buffer"

# Run Buffer
arcpy.analysis.Buffer(roads, output, "100 Feet")

Set tool parameters

Each geoprocessing tool has a fixed set of parameters that provide the tool with the information it needs to run. When a tool is used in Python, its parameter values must be correctly set so it can run when the script is run. Once a valid set of parameter values is provided, the tool is ready to be run. Most parameters can be specified as strings. Some complex parameters can be specified as objects that are easier to work with than long strings.

Strings are text that uniquely identifies a parameter value, such as a path to a dataset or a keyword. In the following code example, input and output parameters are defined for the Buffer tool. Note that the tool name is appended with its toolbox alias. In the example, two string variables are used to define the input and output parameters to make the call to the tool easier to read.

Most geoprocessing tools include both required and optional arguments. Oftentimes, there are cases in which there are many optional arguments that do not need to be specified. There are a couple of strategies for dealing with these unused arguments. One is to keep all optional arguments in order, and reference those you don't need as either an empty string "", a pound sign "#", or with a None type.

import arcpy"schools", "school_id", "LONG", "", "", "", "", "NON_NULLABLE")
A second strategy is to use keyword arguments and use the argument name to assign a value. Using keyword arguments allows you to skip unused optional arguments or specify them in a different order.

import arcpy"schools", "school_id", "LONG", field_is_nullable="NON_NULLABLE")

Tool output

ArcPy returns the output values of a tool as a Result object. The Result object maintains information about how a tool was run, including messages, parameters, and output dataset paths and values. These results can be maintained even after several other tools have been run.

The following examples show how to get the output from a Result object after a geoprocessing tool has run:

import arcpy

arcpy.env.workspace = "c:/city/data.gdb"

# Geoprocessing tools return a Result object 
result ="roads", "urban_roads")

# A print function will display the string representation of the output.

# A Result object can be indexed to get the output value.
result_value = result[0]

If you're creating output that is just an intermediate stage in a larger workflow, the output argument can be omitted to allow the tool to create a unique path and name for the output. This can be accomplished by setting the output either to "#" or None, or if the output would be the last argument used, skip it entirely. In each case, the returned value is the full path to the new data source.

import arcpy
arcpy.env.workspace = "c:/city/data.gdb"

result ="roads", "#")

Build a geoprocessing workflow

ArcPy has a number of useful classes and functions that help you extend your geoprocessing script beyond running tools sequentially. Functions can be used to list certain datasets to perform batch processing, describe a dataset to read its properties programmatically, or perform many other useful geoprocessing tasks.

Learn more about building geoprocessing scripts with ArcPy functions