4

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?

2
  • Here is a link to a similar issue with the same error. The one answer on the page suggests importing the arcinfo module.
    – TsvGis
    Jun 16, 2015 at 21:31
  • I have "Editor' whatever the latest name for this, so results as expected. gp.setProduct("ArcInfo") error when run from ArcGIS when I added arcinfo, Tool not licensed in stand alone mode. I am gutted, it means that there is no way to use multiple processors for some time consuming tasks, where extensions involved
    – FelixIP
    Jun 16, 2015 at 22:14

2 Answers 2

8

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.

9
  • I don' understand how adding arcpy.CheckInExtension("Spatial") at the end of the script can help, if script never gets to the end. Yes, I applied restarting 'approach' multiple times, not helping. Can it be the type of license, that is floating?
    – FelixIP
    Jun 16, 2015 at 22:21
  • Sorry, the whole point of this answer has been missed.. you've already done everything you need and should be getting your extension (well, you need to add arcpy.CheckOutExtension("Spatial") to actually get the extension rather than arcpy.CheckExtension("Spatial") which will only tell you if it is available) but Ken already said that. The point is that the standalone script can consume a 2nd extension license to your ArcGis - to me this is a bug but it may be intentional. The text gives a guide on what to look for to free up the license from ArcGis so you standalone can grab it. Jun 16, 2015 at 22:28
  • As you put it 'intentional' bug, no matter what I do, and number of licenses available
    – FelixIP
    Jun 17, 2015 at 0:10
  • Can you edit your question with your current code please. Have you tried restarting your computer? Jun 17, 2015 at 0:21
  • In reply to your answer I said "Yes, I applied restarting 'approach' multiple times, not helping". Will post latest script later. Did you manage to make it work from stand-alone script? Modify your Q, I am more than happy to accept it. Just try my example
    – FelixIP
    Jun 17, 2015 at 0:37
3

I believe you also have to check out the extension.

import arcpy
import sys

if arcpy.CheckExtension("Spatial") == "Available":
    arcpy.CheckOutExtension("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")
else:
    arcpy.AddMessage("No Spatial Analyst Extension avaialable.")
    sys.exit(0)
5
  • Doesn't work. Message is the same: The tool is not licensed
    – FelixIP
    Jun 16, 2015 at 21:00
  • Do you have more than one Spatial Analyst license? Your open ArcMap session might be holding the extension license.
    – Ken
    Jun 16, 2015 at 21:05
  • I have floating license. I've tried to quit ArcGIS and I can see that there is one available, still no luck. License available, tool not licensed
    – FelixIP
    Jun 16, 2015 at 21:12
  • @FelixIP Did you ever solve the problem? Just wondering. I have tried everything on stackexchange and it doesnot work.
    – pnkjmndhl
    May 4, 2017 at 16:15
  • Close all arcgis things on the computer, kill relevant services, run stand-alone script
    – FelixIP
    May 4, 2017 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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