3

Previously on the following post:

How to get an extent for Raster using python

A user edited the original Python script by adding the following code:

from arcpy.sa import *

Why did he do this and what exactly is it doing? I also received an error when I tried running the original script w/o the edit.

7

I will take a stab at this. That line is what is called an import statement in Python. This basically tells python to go load all the modules in the arcpy.sa path.

Here is more about the important statement:

http://docs.python.org/release/2.5.2/ref/import.html

Here is the path that Python know about to load the modules if you have a default install:

C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\sa

So in short that statement just tells Python were to find the spatial analyst classes that you are using in your Python file.

Additional Info:

Here is a pretty good explanation from the technical side.

http://www.stereoplex.com/blog/understanding-imports-and-pythonpath

The reason you received an error without it is that Python did not know where to find the class you are trying to use.

| improve this answer | |
4

The main reason ESRI suggests doing this is to import all the overloaded operators for use in raster algebra. If you aren't using them then just import sa by itself.

| improve this answer | |
  • This is a good point, which my comment above did not really cover. In the online help itself, it says "Using this import method makes it possible to access Spatial Analyst functionality without providing a name space and imports overloaded operators, which allows rasters to be used with operators." It certainly saves you a lot of un-needed typing. – RyanKDalton Nov 14 '13 at 21:22
  • 1
    Yes. That documentese sentence about "Using this import method..." is an overly vague way of saying "do this because then your Python code will look like old-style map algebra." – Jason Scheirer Nov 14 '13 at 21:27
  • @JasonScheirer I think you should provide that as a separate Answer (and also drop a note to the documenter:-) – PolyGeo Nov 14 '13 at 21:45
  • Still bad practice :) – Nathan W Nov 14 '13 at 23:25
  • 1
    @NathanW Hence not posting as an answer – Jason Scheirer Nov 15 '13 at 1:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.