This is a guest post from Ann Stark (Twitter @StarkAnn) at The GIS Studio Blog. Ann has put together some really nice posts on the subject of using Python with ArcGIS Desktop. Check out her blog!
Over the years I have had amassed a collection of python scripts written in previous versions of ArcMap. They all start out with the import arcgisscripting command and are full of gp statements. And they are still useful scripts, so I have spent some time upgrading them to the new ArcPy site-package for ArcMap version 10. (What is ArcPy?) (More here)
I developed a checklist to help me remember where to look for switching things up and thought I’d share it with others that might have the same task ahead of them. Here’s the checklist.
1. Replace the import statement and remove the geoprocessing creation statement. In other words, remove this
import arcgisscripting
gp = arcgisscripting()
and replace it with
import arcpy
Also in this step I remove any add toolboxes statements that I might have included. No need to specify any toolboxes in v10!
2. Replace all gp. with arcpy.
I do this with a find/replace command in my code editor. For example
gp.exists(myGDB)
now becomes
arcpy.Exists(myGDB)
3. Check your capitalization.
Previous versions of arcgisscripting allowed for sloppy capitalization. Now it has to be exact and this can be a real pain to find all the mismatched cases. gp.addmessage worked before but now it has to be arcpy.AddMessage. And note the capital “Exists” in the example above. Now that I’ve been working with arcpy a bit I find my eye is trained, however in the beginning this was a common bug to find in my scripts and repair.
4. The result tool is different.
Results were pretty straightforward up until ArcMap 9.3. At that point they became a little more tricky.
Now, many geoprocessing tools return a result object of the derived output dataset. A print statement will display the string representation of the output.
>>result = arcpy.GetCount_management(“myLayer”)
>>result
<Result ’879′>
>>print result
879
The result object’s getOutput method returns values as a unicode string. To convert to a different Python type, use built-in Python functions: str(), int(), long(), float().
>>resultValue = result.getOutput(0)
>>resultValue
u’879′
>>count = int(resultValue)
>>print count
879
In your code you can smash it all together to look like this and save a few lines. Although this makes your code less readable by other novice users that might need or want to modify the script.
>>ItemCount = int(arcpy.GetCount_management(“myLayer”).getOutput(0))
>>ItemCount
879
5. Replace any del gp statements.
I had been taught to always delete the gp module any time you ended the script either at the very end or in an try/except statement. I was told that it cleaned up any memory usage. I don’t know how true it is, but what can it hurt, right? So my scripts are littered with
del gp
statements. Now, if you’ve never created the geoprocessor in the first place (see step 1), these are little bombs waiting to go off when your script runs into them. You can’t delete what doesn’t exist. Be sure to find them all and replace them with a
del arcpy
6. The overwrite tool is different.
You’ll need to find and update all uses of it that you might have. Previously you had something like this:
gp.overwrite = 1
where 1 is true and 0 is false. Now you can do something like:
arcpy.env.overwriteOutput = True
or even
from arcpy import env
env.overwriteOutput = True
7. Import the env module from arcpy.
This isn’t a requirement, however I tend to use the environment settings fairly often in my scripts so implicitly importing the env module saves a bit of typing. In the example above you can use the shorter env.overwriteOutput = true statement. Additionally, another common one that I use is the workspace setting. By importing the env module you can then use a short
env.workspace = ‘c:/my/path’
statement. If you go this route be sure to remember to put
from arcpy import env
up near the top of your scripts with the other import statements in order for these shorter statements to work.
That’s it! At least for now. If I find I run into some more common things to remember I’ll update this posting. And please let me know if you have run into any tips while updating your scripts in the comments below.
Want to learn more about using Python with ArcGIS 10 and ArcGIS 10.1? We have a number of instructor led and self paced training opportunities.
Python ArcGIS Programming Bootcamp
Next Session: July 9th – August 10th
GIS Programming 101 for ArcGIS 10
GIS Programming 201 for ArcGIS 10
ArcGIS Programming with Python Bundle