1

I wonder if we can use arcpy.GetParameterAsText(0) in Python IDLE to get a parameter from a user?

I am using ArcGIS Desktop 10.4 and Python 2.7.10 that comes with ArcGIS. Actually I want to know how to use arcpy.GetParameterAsText() to get an input from a user or I should use input() function insted. If so, what is the use of arcpy.GetParameterAsText()?

import os
import arcpy

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

in_featureclass = arcpy.GetParameterAsText(1)
out_workspace = arcpy.GetParameterAsText(2)

out_featureclass = os.path.join(out_workspace,
                                os.path.basename(in_featureclass))


arcpy.CopyFeatures_management(in_featureclass, out_featureclass)

and I got the following error:

Traceback (most recent call last):

  File "C:/Data/s2.py", line 14, in <module>

arcpy.CopyFeatures_management(in_featureclass, out_featureclass)

  File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\management.py", 
line 2335, in CopyFeatures
    raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000735: Input Features: Value is required
ERROR 000735: Output Feature Class: Value is required
Failed to execute (CopyFeatures).
3
  • Are you writing something for use in ArcGIS such as a toolbox, or are you asking about command line options? If the latter, it depends on the version of Python (2.7, used with Desktop, or 3.x, used with Pro). In Python 3, it's myvar = input("string prompt for information") and Python2, myvar = raw_input("string prompt for information")
    – SMiller
    Commented Mar 31, 2019 at 13:06
  • Thanks smiler. I want to know how should I use arcpy.GetParameterAsText()? Should I create a toolbox first in ArcGIS?
    – Hasan77
    Commented Mar 31, 2019 at 13:12
  • It's unclear what you are asking. Please edit your question to clarify where exactly you will be prompting users for input and what version of software you are using. The comment above is primarily for when the input request is completely outside of ArcGIS.
    – SMiller
    Commented Mar 31, 2019 at 13:15

2 Answers 2

1

Your Script is ok and working as expected. However by looking your error's i can say you are passing wrong parameters. For debugging i would suggest you few modification in your script.

import os
import arcpy

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

in_featureclass = arcpy.GetParameterAsText(1)
out_workspace = arcpy.GetParameterAsText(2)

# Printing all input parameters.
arcpy.AddMessage("Workspace--"+ arcpy.GetParameterAsText(0)   )
arcpy.AddMessage("in_featureclass--"+ arcpy.GetParameterAsText(1))
arcpy.AddMessage("out_workspace--"+ arcpy.GetParameterAsText(2))

out_featureclass = os.path.join(out_workspace, os.path.basename(in_featureclass))

arcpy.AddMessage("out_featureclass --"+ out_featureclass)

arcpy.CopyFeatures_management(in_featureclass, out_featureclass)

Finally check the result window from arcCatalog or arcMap. You should see this kind of log. enter image description here

Also for taking input parameters from user, you could create script parameters in you toolbox script like this. enter image description here

This is how i passed my parameters and it ran successfully.enter image description here

2
  • Thanks Bhashkar. I ran your code again and got exactly the same error.
    – Hasan77
    Commented Mar 31, 2019 at 13:52
  • How are you passing parameters. Please check the parameters, I have added a screen shot, how i added parameters in my script. Furthermore all input parameters are required, You should not miss single one. Commented Mar 31, 2019 at 13:53
1

In the arcpy module, arcpy.GetParameterAsText() is used to create python script tools in the toolbox of ArcMap or ArcGIS Pro.

Following documentaion will show you how to create a script tool: http://desktop.arcgis.com/en/arcmap/10.3/analyze/creating-tools/adding-a-script-tool.htm

And following documentation will explain how arcpy.GetParameterAsText() are used: http://desktop.arcgis.com/en/arcmap/10.3/analyze/creating-tools/setting-script-tool-parameters.htm

If your objective it to run the python script in python IDLE then I would suggest you to use the input method since arcpy.GetParameterAsText() will not work in IDLE.

Or your can just hardcode the variables being used. For example:

import os
import arcpy

arcpy.env.workspace = r"C:/path/to/geodatabase.gdb"


in_featureclass = r"C:/path/to/geodatabase.gdb/featureClass"
out_workspace = r"C:/path/to/geodatabase1.gdb"

out_featureclass = os.path.join(out_workspace,
                                os.path.basename(in_featureclass))


arcpy.CopyFeatures_management(in_featureclass, out_featureclass)
1
  • Thanks Gurminder. That hepled. Is there an arcpy documentation that teachs step by step?
    – Hasan77
    Commented Apr 3, 2019 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.