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 (trying both arcpy.SetParameter
and return
). However, when I run the tool in a separate script, the return value is None
.
I use the following code to import the toolbox and run the code:
arcpy.ImportToolbox(pyt_path, "myToolbox")
result = arcpy.myToolbox.HelloWorld()
Below is an example .py script for the tool above
import arcpy
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"""
param1 = arcpy.Parameter(
displayName="output",
name="outparam",
datatype="GPString",
parameterType="Derived",
direction="Output")
return [param1]
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.SetParameter(0, "Hello world")
return "Hello world"
def postExecute(self, parameters):
"""This method takes place after outputs are processed and
added to the display."""
return
Below is my .pyt file
from HelloWorld import HelloWorld
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the .pyt file)."""
self.label = 'Test TBX'
self.alias = 'TestTBX'
# List of Tool classes associated with this toolbox
self.tools = [HelloWorld]
Here is a regular arcpy example using their Spatial Analyst toolbox. I would like to get a return value as well with my custom tool.
# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import Basin
# Set environment settings
env.workspace = "C:/Path/To/Data"
# Set local variables
inFlowDirectionRaster = "flowdir"
# Execute FlowDirection
outBasin = Basin(inFlowDirectionRaster)
1 Answer 1
Your code worked fine for me returning the text Hello world. My script was:
arcpy.ImportToolbox(r"C:\Temp\ORN\t2.pyt","TestTBX" )
res = arcpy.TestTBX.HelloWorld()
print(res.getOutput(0))
The only things I did was to ensure the HelloWorld.py file was in the same folder as the PYT file and removed the text after the return statement in the execute function; you do not need that as you are passing out the text through the SetParameter(), which you are now correctly defining.
Explore related questions
See similar questions with these tags.
getParameterInfo
function. Other than that, I am unsure of how to move forward.