Script working in Python window but not script tool

600
13
01-29-2020 12:44 PM
GraceKeyes
New Contributor II

I'm new to Python but am working with a very simple script to convert elevation units from meters to feet. It works in the Python window but not as a script tool. Below is my script. 


import arcpy
arcpy.env.workspace = "C:\Users\keyesge\Desktop"

inputShp = "JunctionElev"
fieldName = "RASTERVALU"
expression = "!RASTERVALU! * 3.28084"

arcpy.CalculateField_management(inputShp, fieldName, expression,"PYTHON")

Any input is appreciated.

0 Kudos
13 Replies
DanPatterson_Retired
MVP Esteemed Contributor
pth = "C:\Users\keyesge\Desktop"
  File "<ipython-input-1-507cf8adcb68>", line 1
    pth = "C:\Users\keyesge\Desktop"
         ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

# ---- use raw encoding

pth = r"C:\Users\keyesge\Desktop"  # ---- a little r goes a long way

This is error I got when I tried your path... I would start there

0 Kudos
DavidPike
MVP Frequent Contributor

Do you mean it to work as a functioning tool with different data inputs?

You need to replace your hard coded stuff with parameters

0 Kudos
GraceKeyes
New Contributor II

Different data inputs?

0 Kudos
DavidPike
MVP Frequent Contributor

Do you plan to use the tool on different tables and field names or is it just to constantly update the one table?

If that's the case, just change the path as Dan Patterson has written out for you.

0 Kudos
GraceKeyes
New Contributor II

Yes different tables.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

For future reference, it is always helpful to post the error message and traceback so GeoNet users have something specific to look at in terms of what is going wrong.

GraceKeyes
New Contributor II

I didn't receive an error message... The field I was trying to update wasn't reflecting my script. The script tool gave me a message that the tool was successful, but nothing changed in the table...

0 Kudos
JoeBorgione
MVP Esteemed Contributor

As Dan indicates:

arcpy.env.workspace = "C:\Users\keyesge\Desktop"

should be:

arcpy.env.workspaces = r'C:\Users\keyesge\Desktop'

Using single or double quotes is just a matter of personal style/preference.

That should just about do it....
DavidPike
MVP Frequent Contributor

Just a typo for workspace at the end Joe.

0 Kudos