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?
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?
Yes, I am trying to build a toolbox. What you mean by input value?
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.
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.
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