How to return value from ArcGIS Pro custom tool (.py)?

427
4
02-29-2024 02:49 PM
Labels (1)
uasdev
by
New Contributor

How do we return a value from a custom .py tool, so that when I run the tool in another script, it will return a value (such as a number, string, or tuple)?

Notice in the execute class function, I attempt to return a tuple of values. However, when I run the tool in a separate script, the return value is empty.

I use the following code to import the toolbox and run the code:

```

arcpy.ImportToolbox(pyt_path, "myToolbox")
arcpy.myToolbox.HelloWorld()
```
 

Below is an example .py script for the tool above

```

import arcpy

from multiprocessing import Pool

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

    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."""      
        arcpy.AddMessage("Hello World!")
        # arcpy.AddMessage("Hello Jupiter!")
        return ("Hello world", 1, True)

    def postExecute(self, parameters):
        """This method takes place after outputs are processed and
        added to the display."""
        return

```

0 Kudos
4 Replies
GeeteshSingh07
Occasional Contributor II

Hi @uasdev,  this is how I execute my codes:

The following is my .py file:

import arcpy


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

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


class HelloWorld:
    def __init__(self):
        self.label = "HelloWorld"
        self.description = ""

    def getParameterInfo(self):
        """Define the tool parameters."""
        params = None
        return params

    def isLicensed(self):
        """Set whether the 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."""
        out_str = "This is String"
        out_tuple = [1,2,3,4,5]
        arcpy.AddMessage("Hello")
        print(out_str, out_tuple)

    def postExecute(self, parameters):
        """This method takes place after outputs are processed and
        added to the display."""
        return

 

If you want to print, string/tuple or any other data type:

GeeteshSingh07_0-1709266772009.png

If you want to return arcpy.message, then, instead of 'print', use 'return':

    def execute(self, parameters, messages):
        """The source code of the tool."""
        out_str = "This is String"
        out_tuple = [1,2,3,4,5]
        arcpy.AddMessage("Hello")
        return

GeeteshSingh07_1-1709266852927.png

 

How did I debug?

Using return statement, python understands there is an output, but it returns empty.

GeeteshSingh07_2-1709267196441.png

GeeteshSingh07_3-1709267235027.png

 

I am not sure, if this is expected behavior or not, but this is how I run my code.

Best, Geetesh

 

 

0 Kudos
uasdev
by
New Contributor

Thanks Geetesh! This should work for my application. 

Appreciate the help.

Best,

Alex

0 Kudos
GeeteshSingh07
Occasional Contributor II

That's great! @uasdev 

0 Kudos
uasdev
by
New Contributor

HI @GeeteshSingh07 , this option does not work for me. The return value is None when I try to return variables within the execute function in arcgis tool.

0 Kudos