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:
```
Below is an example .py script for the tool above
```
import arcpy
```
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:
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
How did I debug?
Using return statement, python understands there is an output, but it returns empty.
I am not sure, if this is expected behavior or not, but this is how I run my code.
Best, Geetesh
Thanks Geetesh! This should work for my application.
Appreciate the help.
Best,
Alex
That's great! @uasdev
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.