ArcGIS Pro Python toolbox 1

Dr. Huidae Cho
Institute for Environmental and Spatial Analysis...University of North Georgia

1   Python toolbox

  • Single Python script with a .pyt extension
  • One class for the toolbox
  • One class for each tool
  • Source code in any editor
  • Additional capabilities
    • Value tables
    • Composite data types
    • Custom license checking

2   Hello World in Python toolbox

HelloWorldPython.pyt

import arcpy

class Toolbox(object):
    def __init__(self):
        '''Define the toolbox (the name of the toolbox is the name of the
        .pyt file).'''
        self.label = 'HelloWorld Python Toolbox'
        self.alias = 'HelloWorldPythonToolbox'

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

class HelloWorldPython(object):
    def __init__(self):
        '''Define the tool (tool name is the name of the class).'''
        self.label = 'Hello World Python'
        self.description = 'This is Hello World Python!'
        self.canRunInBackground = False

    def getParameterInfo(self):
        '''Define parameter definitions'''
        param1 = arcpy.Parameter(
            displayName='Param 1',
            name='param1',
            datatype='GPString',
            parameterType='Required',
            direction='Input')

        param2 = arcpy.Parameter(
            displayName='Param 2',
            name='param2',
            datatype='GPString',
            parameterType='Required',
            direction='Input')

        params = [param1, param2]
        return params

    def isLicensed(self):
        '''Set whether tool is licensed to execute.'''
        return True

    def updateParameters(self, parameters):
        '''Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed.'''
        return

    def updateMessages(self, parameters):
        '''Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation.'''
        return

    def execute(self, parameters, messages):
        '''The source code of the tool.'''
        arcpy.AddError(parameters[0].valueAsText)
        arcpy.AddWarning(parameters[1].valueAsText)
        arcpy.AddMessage('Hello World Python!')
        print('Done!')
        return

2.1   Try Hello World Python!

From ArcGIS Pro

From the command line (cmd.exe) “after” closing ArcGIS Pro

hello_world.py

import arcpy

arcpy.ImportToolbox('HelloWorldPython.pyt')
arcpy.HelloWorldPython_HelloWorldPythonToolbox('Hello', 'World')

Run

python hello_world.py