I am using arc 10.4.1, I am having trouble adding the environment settings into my script. I want the user to set the environment, without having to hit this button and setting it:
I have attempted:
env.workspace = arcpy.GetParameterAsText(0)
It runs fine, but exports to the folder outside the one I set.
1 Answer 1
Here's my script, sets the Environment Workspace based on a folder/geodatabase, and then copies a pre-existing feature class to that workspace (based on the fact that I don't set a location for the output, just a feature class name).
import arcpy
arcpy.env.workspace = arcpy.GetParameter(0)
arcpy.AddMessage(arcpy.env.workspace)
# Copy existing feature class to location specified above
arcpy.CopyFeatures_management(r"D:\GIS\SE\GISSE.gdb\testPoint", "abcd")
Note: I tend to use arcpy.GetParameter()
rather than arcpy.GetParameterAsText()
for my tool parameters as this allows the objects to be referenced rather than receiving just a string. I only use arcpy.GetParameterAsText()
if I specifically require/expect a string, or if an object isn't available. I have tested both in this case, and I get the same result with both.
In your script tool properties, on the Parameters tab, set your Data type to "Workspace" to enable browsing to a folder or geodatabase workspace.
To test, run the tool and browse to or enter a workspace path and hit OK
By browsing to this new location in Catalog window, I can see that the new feature class was created in the workspace specified by user entry in the tool.
Explore related questions
See similar questions with these tags.