Checklist for updating from ArcGISScripting to ArcPy

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


now becomes


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 '879'>
>>print result

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)
>>count = int(resultValue)
>>print count

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))

6. 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

7. 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

8.  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.


One thought on “Checklist for updating from ArcGISScripting to ArcPy

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s