using python scripts in models

1954
11
10-18-2011 03:51 AM
YousafHassan
New Contributor II
Hi I am trying to use python script in a model but it is throwing an error message:

ERROR 000539: Runtime error <type 'exceptions.NameError'>: name 'table2' is not defined
Failed to execute (Calculate Field).

I guess, it is about how to use table2 in the CalculateField_management function. I am not sure how I can do it.

I have attached the screen shot of my model and the script. Any help would be really appreciated.

Thanks
Tags (2)
0 Kudos
11 Replies
StacyRendall1
Occasional Contributor III
That is not the way to use a Python script.

You were trying to put a script inside the Field Calculator. The field calculator can have some Python stuff in it, but only basic operators. What is it you want to do inside the tool? The tool you have showing IS the calculateField_management() tool that you have in your script... I.e. you can just do it directly...
0 Kudos
YousafHassan
New Contributor II
Hi

I am trying to do what this example is showing in VB:

http://support.esri.com/en/knowledgebase/techarticles/detail/32482

I am trying to use python instead of VB.
0 Kudos
ChristopherStorer
New Contributor
You have the right idea; you're just making it more complicated than it needs to be.  Try:

arcpy.CalculateField_management(test_poly, "X_COORD", "!Shape!.centroid.x", "PYTHON", "")


I have attached a screenshot below.
0 Kudos
YousafHassan
New Contributor II
You have the right idea; you're just making it more complicated than it needs to be.  Try:

arcpy.CalculateField_management(test_poly, "X_COORD", "!Shape!.centroid.x", "PYTHON", "")


I have attached a screenshot below.


Hi
I've tried that but it is throwing this error:

Executing (Calculate Field): CalculateField BuildingTest Easting !Shape!.centroid.x PYTHON #
Start Time: Wed Oct 19 15:36:21 2011
ERROR 000539: Error running expression: GPVARIANTOBJECT0.centroid.x <type 'exceptions.AttributeError'>: 'str' object has no attribute 'x'
Failed to execute (Calculate Field).
Failed at Wed Oct 19 15:36:21 2011 (Elapsed Time: 0.00 seconds)
0 Kudos
ChristopherStorer
New Contributor
Well that's interesting.  You do have a shape field called Shape, I assume?  Maybe you can attach a one-record copy of this table so we can reproduce the error.
0 Kudos
YousafHassan
New Contributor II
Well that's interesting.  You do have a shape field called Shape, I assume?  Maybe you can attach a one-record copy of this table so we can reproduce the error.


Yes, the Shape field is there but it is called Shape * see the attached screen shot. Also, I have attached a record for you.
0 Kudos
ChristopherStorer
New Contributor
The * is okay, that just means the field is binary, which is a good thing.  The csv export you posted doesn't have a shape field, and doesn't contain the original field formats, so it doesn't help much with determining the problem.  Please see the attached screenshots for proper export technique.
0 Kudos
curtvprice
MVP Esteemed Contributor
Which version, 9.2, 9.3 and 10.x all have different syntax to access the shape field properties.

I recommend you instead of accessing the centroid this way, take an easier path: use the out of the box Feature To Point tool followed by the Add XY Coordinates tool.
0 Kudos
YousafHassan
New Contributor II
Hi

I have attached the zip which contains the shape file. Please have a look.

Thanks.

The * is okay, that just means the field is binary, which is a good thing.  The csv export you posted doesn't have a shape field, and doesn't contain the original field formats, so it doesn't help much with determining the problem.  Please see the attached screenshots for proper export technique.
0 Kudos