Sunday, 15 January 2017

arcpy - Accessing Spatial Analyst extension from stand alone script?


If I run this script:


import arcpy
arcpy.AddMessage(arcpy.CheckExtension("Spatial"))
from arcpy.sa import Con
from arcpy import env
fDir=r'd:\scratch\fdir'
outFolder=r'd:\aerials\images'

env.workspace = outFolder
fDir=arcpy.Raster(fDir)
Gter=Con(fDir> 1,2)
Gter.save("TEST")

from ArcGIS, the output is:


enter image description here


When I run it as stand-alone script, it is very different story:


enter image description here


How do I access Spatial Analyst extension from a stand alone script?




Answer



The method I use is:


if arcpy.CheckExtension("Spatial") == "Available":
arcpy.AddMessage("Checking out Spatial")
arcpy.CheckOutExtension("Spatial")
else:
arcpy.AddError("Unable to get spatial analyst extension")
arcpy.AddMessage(arcpy.GetMessages(0))
sys.exit(0)


and then at the end of your script:


arcpy.CheckInExtension("Spatial")

which is generically what Ken said, the issue here seems to be that for some external reason arcpy cannot get a license... when using arcpy.sa in a standalone script it is possible to have more than one license checked out (perhaps that's a bug worth reporting to Esri).. check your task manager to ensure there's not ArcCatalog/ArcMap that's half crashed and still holding a license, script windows that have not been exited and also editors that have imported arcpy (PyWin does this if you import arcpy in the interactive window).


Worst case restart your computer... it's amazing how many things that fixes.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...