Import the Spatial Analyst module

Available with Spatial Analyst license.

Spatial Analyst ( is a module of the ArcPy site package. The best way to access the functionality of the ArcGIS Spatial Analyst extension, including tools, operators, functions, and classes, is to import from the sa module. Using this import method makes it possible to access this functionality without providing a name space and imports overloaded operators, which allows rasters to be used with operators.

The recommended sequence of imports to use Spatial Analyst functionality is as follows:

import arcpy
from arcpy import env
from import *

This approach allows you to do the following:

  • Set and get environment setting values directly from the env class.

    env.workspace = "c:/base/data.gdb"
  • Run geoprocessing tools and functionality directly from arcpy.

    arcpy.Buffer_analysis("infeatures", "outbuffer", 5000)
  • Use map algebra without having to prefix every tool and class with

    outRas = Sin("inraster1") + Raster("inraster2") + 8

To learn more about your options when importing ArcPy, map algebra, or other functionality, see Import ArcPy.


In Python (or when in the Python window and the extension have not already been enabled), you need to check out an ArcGIS Spatial Analyst extension license before running a tool.

# Check out the ArcGIS Spatial Analyst
# extension license

Interactive Python experience customization

The Python window (or any other interactive Python interpreter) recognizes the system environment variable PYTHONSTARTUP. If PYTHONSTARTUP has been previously set to a Python file, Python will automatically run that file's code when the Python window is opened. This is a useful way to ensure that your Python window experience starts with all your commonly used Python utilities preloaded.

# File:
# Description: Used to customize the state of the Python startup environment 
#   upon startup of ArcGIS application
#   Can specify module imports, variables, messages
# Requirements: Spatial Analyst Extension

# Print to screen
print("import os\nimport sys\nimport arcpy\nfrom arcpy import env\nfrom import *")

# Imports
import os
import sys
import arcpy
from arcpy import env
from import *

To add the PYTHONSTARTUP environment setting, do the following:

  1. On your computer, locate and open System Properties.
  2. Click the Advanced tab and click Environment Variables.
  3. Under System variables, click New.
  4. Add PYTHONSTARTUP to Variable name.
  5. Add the path of the Python file to Variable value and click OK.
  6. Click OK.

Related topics