ArcGIS for Desktop

  • Documentation
  • Pricing
  • Support

  • My Profile
  • Help
  • Sign Out
ArcGIS for Desktop

ArcGIS Online

The mapping platform for your organization

ArcGIS for Desktop

A complete professional GIS

ArcGIS for Server

GIS in your enterprise

ArcGIS for Developers

Tools to build location-aware apps

ArcGIS Solutions

Free template maps and apps for your industry

ArcGIS Marketplace

Get apps and data for your organization

  • Documentation
  • Pricing
  • Support
Esri
  • Sign In
user
  • My Profile
  • Sign Out

Help

  • Home
  • Get Started
  • Map
  • Analyze
  • Manage Data
  • Tools
  • More...

What is a Python toolbox?

  • Creating a Python toolbox
  • A Python toolbox example

Python toolboxes Python Toolbox are geoprocessing toolboxes that are created entirely in Python. A Python toolbox and the tools contained within look, act, and work just like toolboxes and tools created in any other way. A Python toolbox (.pyt) is simply an ASCII-based file that defines a toolbox and one or more tools.

Once created, tools in a Python toolbox provide many advantages:

  • Creating a Python toolbox allows you to take advantage of your Python knowledge and quickly proto-type and create fully functional geoprocessing tools.
  • The tool that you create is an integral part of geoprocessing, just like a system tool—you can open it from the Search or Catalog window, use it in ModelBuilder and the Python window, and call it from scripts.
  • You can write messages to the Results window and progress dialog box.
  • Using built-in documentation tools, you can provide documentation.
  • When the script is run as a script tool, arcpy is fully aware of the application (such as ArcMap) it was called from. Settings made in the application, such as arcpy.env.overwriteOutput and arcpy.env.scratchWorkspace, are available from ArcPy in your script tool.

Creating a Python toolbox

A Python toolbox can be created by right-clicking the folder in which you want to create the new toolbox, then click New > Python Toolbox.

Initially, the Python toolbox will include a Python class named Toolbox that defines characteristics of the toolbox and a second Python class named Tool that provides a stubbed out geoprocessing tool.

Getting started

Getting started

Creating a new Python toolbox

Editing a Python toolbox

Define your tool

Defining a tool

Define your tool's parameters

Defining parameters in a Python toolbox

Defining parameter data types in a Python toolbox

Customize tool behavior

Customizing tool behavior in a Python toolbox

Updating schema in a Python toolbox

Controlling license behavior in a Python toolbox

Write your tool's 'source code'

Accessing parameters within a Python toolbox

Writing messages in a Python toolbox

Document your tool

Documenting a tool in a Python toolbox

A Python toolbox example

Below is a working example of a Python toolbox containing a single tool. The tool, named CalculateSinuosity, adds a field and calculates the sinuosity of the feature—the sinuosity being a measure of how a line bends.

Note:

To use this tool, copy and paste the sample code into any editor, such as NotePad, and save the file with a .pyt extension.

import arcpy

class Toolbox(object):
    def __init__(self):
        self.label =  "Sinuosity toolbox"
        self.alias  = "sinuosity"

        # List of tool classes associated with this toolbox
        self.tools = [CalculateSinuosity] 

class CalculateSinuosity(object):
    def __init__(self):
        self.label       = "Calculate Sinuosity"
        self.description = "Sinuosity measures the amount that a river " + \
                           "meanders within its valley, calculated by " + \
                           "dividing total stream length by valley length."

    def getParameterInfo(self):
        #Define parameter definitions

        # Input Features parameter
        in_features = arcpy.Parameter(
            displayName="Input Features",
            name="in_features",
            datatype="GPFeatureLayer",
            parameterType="Required",
            direction="Input")
        
        in_features.filter.list = ["Polyline"]

        # Sinuosity Field parameter
        sinuosity_field = arcpy.Parameter(
            displayName="Sinuosity Field",
            name="sinuosity_field",
            datatype="Field",
            parameterType="Optional",
            direction="Input")
        
        sinuosity_field.value = "sinuosity"
        
        # Derived Output Features parameter
        out_features = arcpy.Parameter(
            displayName="Output Features",
            name="out_features",
            datatype="GPFeatureLayer",
            parameterType="Derived",
            direction="Output")
        
        out_features.parameterDependencies = [in_features.name]
        out_features.schema.clone = True

        parameters = [in_features, sinuosity_field, out_features]
        
        return parameters

    def isLicensed(self): #optional
        return True

    def updateParameters(self, parameters): #optional
        if parameters[0].altered:
            parameters[1].value = arcpy.ValidateFieldName(parameters[1].value,
                                                          parameters[0].value)
        return

    def updateMessages(self, parameters): #optional
        return

    def execute(self, parameters, messages):
        inFeatures  = parameters[0].valueAsText
        fieldName   = parameters[1].valueAsText
        
        if fieldName in ["#", "", None]:
            fieldName = "sinuosity"

        arcpy.AddField_management(inFeatures, fieldName, 'DOUBLE')

        expression = '''
import math
def getSinuosity(shape):
    length = shape.length
    d = math.sqrt((shape.firstPoint.X - shape.lastPoint.X) ** 2 +
                  (shape.firstPoint.Y - shape.lastPoint.Y) ** 2)
    return d/length
'''

        arcpy.CalculateField_management(inFeatures,
                                        fieldName,
                                        'getSinuosity(!shape!)',
                                        'PYTHON_9.3',
                                        expression)

Related Topics

  • A quick tour of creating tools with Python
  • Comparing custom and Python toolboxes
Feedback on this topic?

ArcGIS for Desktop

  • Home
  • Documentation
  • Pricing
  • Support

ArcGIS Platform

  • ArcGIS Online
  • ArcGIS for Desktop
  • ArcGIS for Server
  • ArcGIS for Developers
  • ArcGIS Solutions
  • ArcGIS Marketplace

About Esri

  • About Us
  • Careers
  • Insiders Blog
  • User Conference
  • Developer Summit
Esri
© Copyright 2016 Environmental Systems Research Institute, Inc. | Privacy | Legal