Python script runs in PythonWin, but not in a Toolbox

2301
4
07-15-2013 06:12 AM
JacobDrvar
New Contributor II
I am new to python scripting.  I began by creating a script to clip feature classes.  The script works in PythonWin, but fails when added to a toolbox.  The script is as follows:

#Import system modules
import arcpy
from arcpy import env

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

#Variables
in_features = "Sales_2006.shp"
clip_features = "EagleHarborSales_temp.shp"
out_feature_class = "Output_temp5.shp"

#Clip the Sales_2006 shapefile to the EagleHarborSales_temp shapefile
arcpy.Clip_analysis(in_features, clip_features, out_feature_class)

#Info
print arcpy.GetMessages()

I then created a toolbox and added the script to the toolbox.  I went through the script properties, created a name, label, checked store relative path names, left always run in the foreground checked, browsed to the script, left run python script in process checked, entered the input, output, and added the optional parameters. Then, I attempted to run the script in toolbox.  I specified the input feature and noticed the default output was my C Drive.  This maybe part or the entire issue.  But, Why would the output be defaulting to the C Drive, when my workspace is on my E Drive?  And how would I fix that problem?  Thanks.
Tags (3)
0 Kudos
4 Replies
XanderBakker
Esri Esteemed Contributor

Hi JD,

To use the script in a toolbox with parameters, you have to adapt the script so it reads the parameters. See the Help on "Accessing parameters in a script tool": ArcGIS Help (10.2, 10.2.1, and 10.2.2)

You will have to use the arcpy.GetParameterAsText() or arcpy.GetParameter() and arcpy.SetParameter() or arcpy.SetParameterAsText() to do so...

Kind regards, Xander

0 Kudos
OwenEarley
Occasional Contributor III

Hi JD,

As Xander points out you will need to modify your script to use the toolbox parameters. If you want to set default values for your script parameters you can do this in the Script Properties:

script-props.png

Owen

www.spatialxp.com.au

0 Kudos
DanPatterson_Retired
MVP Emeritus

Also you environment workspace should use double backslashes, single fore slashes or raw notation...consult python documentation

0 Kudos
MatthiasBuehler1
Frequent Contributor

Hi,

this discussion is about ArcPy ( python in arc gis ), thus not related to CityEngine.

I'd like to move this discussion to an other 'place' to keep the CityEngine place clean.

Though, I don't have admin rights at the moment, so I can't do this.

@ J 😧 Can you try and move this discussion to a better spot ? Otherwise, let me know, then I can organize this.

Matt

0 Kudos