Modifying Python script to work in a toolbox

321
5
07-15-2013 10:00 AM
JacobDrvar
New Contributor II
I am new to python scripting and began by creating a specific tool.  The clip works well in either PythonWin 2.7 or ArcToolbox 10.1. Now, I want to modify the script for any feature class. How would I modify the script to input/clip/output any feature class? GetParameterAsText? Then, I intend to add the script to a toolbox. The script is as follows:

#Import system modules
import arcpy
from arcpy import env

#Workspace
env.workspace = "E:\User\MillageCode"

#Variables
input = "Sales_2006.shp"
clip = "EagleHarborSales_temp.shp"
output = "Output_temp5.shp"

#Clip the Sales_2006 shapefile to the EagleHarborSales_temp shapefile
arcpy.Clip_analysis(input, clip, output)

#Info
print arcpy.GetMessages()


Thanks!

So, I have modified the Python script to include the GetParameterAsText function.  But, I receive an error code that statues values are not defined for the input, clip, and output.  The code now reads:

#Import modules
import arcpy, os, sys
from arcpy import env

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

#Variables
input = arcpy.GetParameterAsText(1)
clip = arcpy.GetParameterAsText(2)
output = arcpy.GetParameterAsText(3)

#Clip
arcpy.Clip_analysis(input, clip, output)

#Info
print arcpy.GetMessages()


Any suggestions on why the script errors out?
Tags (2)
0 Kudos
5 Replies
DanPatterson_Retired
MVP Emeritus
"E:\User\MillageCode"

shouldn't work, you need \\ instead of \ or append r to the front of the path
0 Kudos
JacobDrvar
New Contributor II
"E:\User\MillageCode"

shouldn't work, you need \\ instead of \ or append r to the front of the path


The E:\User\MillageCode path works in both ArcToolbox and PythonWin. 

Any suggestions on the second script?  The error is an ExecuteError: Failed to execute.  Parameters are not valid.  Error 000735: Input, Clip, and Output Features: Value is required.  I thought the values were provided as variables.
0 Kudos
DanPatterson_Retired
MVP Emeritus
print the inputs and outputs to see if what you suggests does indeed work
0 Kudos
JacobDrvar
New Contributor II
Dan,

The desired results were achieved through the first script.  I loaded the output shapefile created in Python vs. running the clip tool in ArcToolbox.  The results were identical.  I just NEED to get the second script up and running.  Thanks.

This is a copy of the results from the Interactive Window.  I just reran the first script. 

Executing: Clip E:\User\MillageCode\Sales_2006.shp E:\User\MillageCode\EagleHarborSales_temp.shp E:\User\MillageCode\Output_temp5.shp #
Start Time: Tue Jul 16 07:35:46 2013
Reading Features...
Cracking Features...
Assembling Features...
Succeeded at Tue Jul 16 07:35:46 2013 (Elapsed Time: 0.00 seconds)
0 Kudos
WesMiller
Regular Contributor III
Are you supplying the script with the requested parameters? In PythonWin there is a place to put in the parameters(Arguments). In toolbox you will have to create the parameters.
0 Kudos