arcpy.GetParameterAsText not passing arguments to the script

1824
4
Jump to solution
05-29-2019 12:47 AM
HaydenWilson2
New Contributor

I have been working on an arcgis tool which takes the co-ordinate system from an input raster and reprojects a vector to the same co-ordinate system.

The script works fine when i hard code the addresses for the workspace and input datasets. But as soon as I try to pass those as parameters in a toolbox it no longer works.

Can anyone here point out what I am doing wrong?

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 = [Tool]class Tool(object):    def __init__(self):        """Define the tool (tool name is the name of the class)."""        self.label = "Test_tool"        self.description = ""        self.canRunInBackground = False     def getParameterInfo(self):        """Define parameter definitions"""        param0 = arcpy.Parameter(            displayName="Input workspace",            name="workspace",            datatype="DEWorkspace",            parameterType="Required",            direction="Input")        param1 = arcpy.Parameter(            displayName="Input classified raster",            name="input_raster",            datatype="GPRasterLayer",            parameterType="Required",            direction="Input")        param2 = arcpy.Parameter(            displayName="Input features",            name="input_features",            datatype="GPFeatureLayer",            parameterType="Required",            direction="Input")         params = [param0, param1, param2]         return params      def execute(self, parameters, messages):        """The source code of the tool."""         # Define some paths/variables        outWorkspace = arcpy.GetParameterAsText(0)        arcpy.env.workspace = outWorkspace         input_raster = arcpy.GetParameterAsText(1)        input_features = arcpy.GetParameterAsText(2)         output_features = outWorkspace + "\\projected.shp"         out_coordinate_system = arcpy.Describe(input_raster).spatialReference         proj_grid = arcpy.Project_management(input_features, output_features, out_coordinate_system)         return

When I run the tool it provides the following error:

Traceback (most recent call last):  File "<string>", line 75, in execute   File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\__init__.py", line 1245, in Describe    return gp.describe(value, data_type)  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 370, in describe     self._gp.Describe(*gp_fixargs(args, True)))OSError: "" does not exist  Failed to execute (Tool).

it seems to me that the input variables of the parameters are not being passed correctly.

Any advice would be very appreciated.

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

It appears that you are using a Python toolbox script (.pyt file) and including GetParametersAsText which works with custom Python script tool.  Comparing custom and Python toolboxes might help clarify some of the differences.

Here is your script with the formatting that Dan Patterson‌ is suggesting:

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 = [Tool]


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

    def getParameterInfo(self):
        """Define parameter definitions"""
        param0 = arcpy.Parameter(
            displayName="Input workspace",
            name="workspace",
            datatype="DEWorkspace",
            parameterType="Required",
            direction="Input")
        
        param1 = arcpy.Parameter(
            displayName="Input classified raster",
            name="input_raster",
            datatype="GPRasterLayer",
            parameterType="Required",
            direction="Input")
        
        param2 = arcpy.Parameter(
            displayName="Input features",
            name="input_features",
            datatype="GPFeatureLayer",
            parameterType="Required",
            direction="Input")
        
        params = [param0, param1, param2]
        return params

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

        # Define some paths/variables
        outWorkspace = arcpy.GetParameterAsText(0)
        arcpy.env.workspace = outWorkspace
        input_raster = arcpy.GetParameterAsText(1)
        input_features = arcpy.GetParameterAsText(2)

        output_features = outWorkspace + "\\projected.shp"

        out_coordinate_system = arcpy.Describe(input_raster).spatialReference
        proj_grid = arcpy.Project_management(input_features, output_features, out_coordinate_system)

        return

Since the parameters are defined inside the Python toolbox script (lines 21-45) versus a "wizard" with the custom script, you don't need the GetParameters as text in lines 51-54.  Try replacing lines 47 to the end with:

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

        # Define some paths/variables
        outWorkspace = parameters[0].valueAsText
        arcpy.env.workspace = outWorkspace
        input_raster = parameters[1].valueAsText
        input_features = parameters[2].valueAsText

        output_features = outWorkspace + "\\projected.shp"

        out_coordinate_system = arcpy.Describe(input_raster).spatialReference
        proj_grid = arcpy.Project_management(input_features, output_features, out_coordinate_system)

        return
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Hope this helps.

View solution in original post

4 Replies
HaydenWilson2
New Contributor

Apologies. I am not sure why the code formatted like that after I posted it.

Here is the code with better formatting:

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 = [Tool]


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

def getParameterInfo(self):
"""Define parameter definitions"""
param0 = arcpy.Parameter(
displayName="Input workspace",
name="workspace",
datatype="DEWorkspace",
parameterType="Required",
direction="Input")
param1 = arcpy.Parameter(
displayName="Input classified raster",
name="input_raster",
datatype="GPRasterLayer",
parameterType="Required",
direction="Input")
param2 = arcpy.Parameter(
displayName="Input features",
name="input_features",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")


params = [param0, param1, param2]

return params

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

# Define some paths/variables
outWorkspace = arcpy.GetParameterAsText(0)
arcpy.env.workspace = outWorkspace
input_raster = arcpy.GetParameterAsText(1)
input_features = arcpy.GetParameterAsText(2)

output_features = outWorkspace + "\\projected.shp"

out_coordinate_system = arcpy.Describe(input_raster).spatialReference
proj_grid = arcpy.Project_management(input_features, output_features, out_coordinate_system)

return

0 Kudos
RandyBurton
MVP Alum

It appears that you are using a Python toolbox script (.pyt file) and including GetParametersAsText which works with custom Python script tool.  Comparing custom and Python toolboxes might help clarify some of the differences.

Here is your script with the formatting that Dan Patterson‌ is suggesting:

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 = [Tool]


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

    def getParameterInfo(self):
        """Define parameter definitions"""
        param0 = arcpy.Parameter(
            displayName="Input workspace",
            name="workspace",
            datatype="DEWorkspace",
            parameterType="Required",
            direction="Input")
        
        param1 = arcpy.Parameter(
            displayName="Input classified raster",
            name="input_raster",
            datatype="GPRasterLayer",
            parameterType="Required",
            direction="Input")
        
        param2 = arcpy.Parameter(
            displayName="Input features",
            name="input_features",
            datatype="GPFeatureLayer",
            parameterType="Required",
            direction="Input")
        
        params = [param0, param1, param2]
        return params

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

        # Define some paths/variables
        outWorkspace = arcpy.GetParameterAsText(0)
        arcpy.env.workspace = outWorkspace
        input_raster = arcpy.GetParameterAsText(1)
        input_features = arcpy.GetParameterAsText(2)

        output_features = outWorkspace + "\\projected.shp"

        out_coordinate_system = arcpy.Describe(input_raster).spatialReference
        proj_grid = arcpy.Project_management(input_features, output_features, out_coordinate_system)

        return

Since the parameters are defined inside the Python toolbox script (lines 21-45) versus a "wizard" with the custom script, you don't need the GetParameters as text in lines 51-54.  Try replacing lines 47 to the end with:

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

        # Define some paths/variables
        outWorkspace = parameters[0].valueAsText
        arcpy.env.workspace = outWorkspace
        input_raster = parameters[1].valueAsText
        input_features = parameters[2].valueAsText

        output_features = outWorkspace + "\\projected.shp"

        out_coordinate_system = arcpy.Describe(input_raster).spatialReference
        proj_grid = arcpy.Project_management(input_features, output_features, out_coordinate_system)

        return
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Hope this helps.

HaydenWilson2
New Contributor

Hi. thank you so much.

That works! 

0 Kudos