How to implement dynamic input and output in Python script

3420
5
07-30-2015 03:01 AM
KarolinaKorzeniowska
Occasional Contributor

I am trying to apply dynamic input and output in my Python script using arcpy.GetParameterasText, but I have an error:

Traceback (most recent call last):

  File "C:\Model.py", line 51, in <module>

    outWorkspace = arcpy.GetParameterAsText(2)

  File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\__init__.py", line 648, in GetParameterAsText

    return gp.getParameterAsText(index)

  File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\geoprocessing\_base.py", line 224, in getParameterAsText

    self._gp.GetParameterAsText(*gp_fixargs(args, True)))

RuntimeError: Object: Error in getting parameter as text

Failed to execute (testing).

I defined my input and output as follow:

# Set the input workspace

arcpy.env.workspace = arcpy.GetParameterAsText(0)

# Set the input data

inRaster = arcpy.GetParameterAsText(1)

# Set the output workspace

outWorkspace = arcpy.GetParameterAsText(2)

What is wrong in my code? Why it cannot read the output workspace?

0 Kudos
5 Replies
WesMiller
Regular Contributor III

What is your input value? I don't see anything wrong in the code you have posted. I assume you are tying this code to a toolbox?

0 Kudos
KarolinaKorzeniowska
Occasional Contributor

Yes, I am trying to build a toolbox. What you mean by input value?

0 Kudos
WesMiller
Regular Contributor III

When you build a tool for each GetParameterAsText should have a corresponding input parameter in the toolbox. arcpy.env.workspace = arcpy.GetParameterAsText(0) would be your first "DataType set to workspace, inRaster = arcpy.GetParameterAsText(1) would be your second "Data Type set to Raster Layer and outWorkspace = arcpy.GetParameterAsText(2) would be your third "Data Type set to workspace. When you open the tool there should be three input areas in the dialog box what are you putting in the third they appear in the order you set them.

KarolinaKorzeniowska
Occasional Contributor

Yes, but actually I do not understand it clearly. I want to have a toolbox with two windows: input data, and output data. When in specify three parameters in "Parameter" window, then I have three windows in my toolbox.

0 Kudos
WesMiller
Regular Contributor III

I think what you want is SetParameterAsText you'll need to still create it as a parameter in your tool but set it to be derived data. It also needs to be of the "Data Type" you intend to output. You'll only want to set it to workspace if you intend to create a folder or geodatabase.

http://resources.arcgis.com/en/help/main/10.2/index.html#//018v00000067000000

0 Kudos