
Python scripting in ArcGIS (mostly using ArcPy) is one of the best ways to automate geoprocessing, manage data, perform analysis, and build custom workflows. Let’s get you oriented.
What is ArcPy?
ArcPy is Esri’s Python site package that lets you access geoprocessing tools, map documents, data management, and spatial analysis tools directly from Python scripts.
You can run scripts:
- Inside ArcGIS Pro’s Python window
- As standalone
.py
scripts (using the ArcGIS Pro Python environment) - Or from ModelBuilder via a Python script tool
What Can You Do with Python in ArcGIS?
- Automate repetitive geoprocessing tasks (clip, buffer, project, etc.)
- Batch process multiple datasets
- Create custom analysis workflows
- Manipulate attribute tables
- Update or convert data formats
- Run scheduled jobs
- Build geoprocessing script tools with user interfaces
Basic ArcPy Script Structure:
pythonCopyEditimport arcpy
# Set workspace
arcpy.env.workspace = r"C:\GIS_Projects\MyProject\Geodatabase.gdb"
# Example: Buffer all parks by 500 meters
input_fc = "Parks"
output_fc = "Buffered_Parks"
buffer_distance = "500 Meters"
arcpy.Buffer_analysis(input_fc, output_fc, buffer_distance)
print("Buffer completed.")
Key ArcPy Functional Areas:
Purpose | Example |
---|---|
Workspace Environment | arcpy.env.workspace = r"C:\Data" |
List Data | arcpy.ListFeatureClasses() , arcpy.ListRasters() |
Geoprocessing Tools | arcpy.Buffer_analysis() , arcpy.Clip_analysis() |
Describe Data | desc = arcpy.Describe("Parcels.shp") |
Update Attribute Tables | Use arcpy.da.UpdateCursor() |
Spatial Analysis | arcpy.sa.Slope() , arcpy.sa.ZonalStatistics() |
Raster Processing | arcpy.Raster() , arcpy.CheckOutExtension("Spatial") |
Example: Batch Buffer Multiple Feature Classes
pythonCopyEditimport arcpy
import os
arcpy.env.workspace = r"C:\GIS_Projects\Data"
output_folder = r"C:\GIS_Projects\Buffered"
fc_list = arcpy.ListFeatureClasses()
for fc in fc_list:
output_fc = os.path.join(output_folder, f"{fc}_buffered")
arcpy.Buffer_analysis(fc, output_fc, "300 Meters")
print(f"{fc} buffered.")
Pro Tips:
- Always use raw strings (r” “) for file paths to avoid backslash errors.
- Use cursors (SearchCursor, UpdateCursor, InsertCursor) for reading and modifying attribute tables.
- Use try/except blocks for error handling in scripts.
- Remember to check out necessary extensions (e.g., Spatial Analyst) before using those tools: pythonCopyEdit
arcpy.CheckOutExtension("Spatial")
Where to Write and Test Python:
- ArcGIS Pro Python Window — for quick commands and testing
- Python IDE (e.g., VS Code, PyCharm) — for full scripts (use ArcGIS Pro’s Conda environment)
- Jupyter Notebooks (included with ArcGIS Pro)