0

I have been posting similar questions recently, but I found a template code for a Python toolbox and I would like more help.

I am trying to create a tool in ArcGIS Pro that takes parameters from users (initially, the geodatabase filename) and shows the report on the screen.

I added to the template the function "getParameter." I thought this function will open up a dialog box for a user to choose a parameter, but it does not for some reason.

The following code has no syntax error. When I add a new Python toolbox and ran this code, it does run, but it says "no parameters," as the screenshot says. What change do I need to make?enter image description here

import arcpy
import os


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

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


class Report(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Tool"
        self.description = ""
        self.canRunInBackground = False


    def getParameter(self):  
        arcpy.env.workspace = arcpy.GetParameterAsText(0)

        featureclasses = arcpy.ListFeatureClasses()
        for fc = in featureclasses:
            print (fc)

    def getParameterInfo(self):
        """Define parameter definitions"""
        params = None
        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."""
        return
5
  • 1
    Did you try returning something for getParameterInfo? pro.arcgis.com/en/pro-app/arcpy/geoprocessing_and_python/… Jan 30 '20 at 21:18
  • So, are you saying that because in my code, it says params = None, it returns nothing? Should I change it to 0 then?
    – Yoshi
    Jan 30 '20 at 21:26
  • The parameter need to be a path to a filename (initially, geodatabase filename.) What datatype should I choose for that?
    – Yoshi
    Jan 30 '20 at 21:35
  • You said that you "have been posting similar questions recently". Please always include links to any GIS SE questions that you mention in a new question so that potential answerers do not have to go looking for them before deciding whether this is actually a non-duplicate question.
    – PolyGeo
    Jan 30 '20 at 22:03
  • Have you reviewed Defining parameters in a Python toolbox?
    – PolyGeo
    Jan 30 '20 at 22:04
2

I suggest you look at the Python Toolbox documentation, particularly:

Your code was not working because:

  • your getParameterInfo method was returning None instead of a list of Parameter definitions
  • Your getParameter method will never get called as it's not part of the python toolbox API
  • Your execute method did nothing.

Here's a working example

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 = "Toolbox"
        self.alias = ""

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


class Report(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Tool"
        self.description = ""
        self.canRunInBackground = True

    def getParameterInfo(self):
        # Define parameter definitions

        params = []

        # First parameter
        param0 = arcpy.Parameter(
            displayName="Input workspace",
            name="in_workspace",
            datatype="DEWorkspace",
            parameterType="Required",
            direction="Input")

        params.append(param0)
        return params


    def execute(self, parameters, messages):
        """The source code of the tool."""

        arcpy.env.workspace = parameters[0].valueAsText

        featureclasses = arcpy.ListFeatureClasses()
        for fc in featureclasses:
            messages.addMessage(fc)
1
  • Thank you, but I just realized I have been barkin at the wrong tree. All this time I was thinking I was supposed to make Python toolbox, but actually I am supposed to make a script toolbox... Thank you tho!
    – Yoshi
    Jan 31 '20 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.