I have a script that requires me to use arcpy.AddToolbox, which I currently have set up for 10.3
arcpy.AddToolbox(r"C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcToolbox\Toolboxes\Spatial Analyst Tools.tbx")
How can I query the version and make the path relative so it will work for other versions, e.g. 10.2 or 10.4 (eventually).
BTW, I thought
import arcpy arcpy.CheckOutExtension("spatial") from arcpy.sa import *
would be enough for me to not have to add the toolbox, but that didn't work. If anyone knows why, I'd be curious. Without the full toolbox, it was crashing on arcpy.ExtractValuesToPoints_sa(randomTemp, elevRaster, randomPtSet, "NONE", "VALUE_ONLY")
Thanks.
cross-post python snippets Developers
Solved! Go to Solution.
When importing arcpy the default systems toolboxes are already loaded (i.e. the toolboxes located at <ArcGIS Install Path>\ArcToolbox\Toolboxes. This was a new functionality introduce at 10.0 with arcpy. Prior to this, when creating the gp object you would need to load each toolbox that prior to using it via a call to gp.AddToolbox. When using a custom toolbox you'd need to call AddToolbox at 9.3 or earlier and ImportToolbox at 10.0 or higher.
The problem you're having with the spatial analyst tools are now part of their own module in the arcpy site package. You would call them as functions in the same manner as when you using the arcpy.mapping or arcpy.na modules.
Importing arcpy
http://desktop.arcgis.com/en/desktop/latest/analyze/python/importing-arcpy.htm
I would assume that the error you're seeing is because you're calling ExtractValuesToPoints_sa and there is no function available with this name. You would either need to call arcpy.sa.ExtractValuesToPoints or if you import all of the functions from the sa module (i.e. import * from sa) you would use only the function name ExtractValuesToPoints. (Note the missing "_sa" part. This was intentionally left out because it isn't part of the function name)
Excellent. Thanks Freddie. And I can see I'll be able to pull the path from that too.
but get install version isn't backward compatible is it? obviously to the start of arcpy?
Good to know....for backwards compatibility.
When importing arcpy the default systems toolboxes are already loaded (i.e. the toolboxes located at <ArcGIS Install Path>\ArcToolbox\Toolboxes. This was a new functionality introduce at 10.0 with arcpy. Prior to this, when creating the gp object you would need to load each toolbox that prior to using it via a call to gp.AddToolbox. When using a custom toolbox you'd need to call AddToolbox at 9.3 or earlier and ImportToolbox at 10.0 or higher.
The problem you're having with the spatial analyst tools are now part of their own module in the arcpy site package. You would call them as functions in the same manner as when you using the arcpy.mapping or arcpy.na modules.
Importing arcpy
http://desktop.arcgis.com/en/desktop/latest/analyze/python/importing-arcpy.htm
I would assume that the error you're seeing is because you're calling ExtractValuesToPoints_sa and there is no function available with this name. You would either need to call arcpy.sa.ExtractValuesToPoints or if you import all of the functions from the sa module (i.e. import * from sa) you would use only the function name ExtractValuesToPoints. (Note the missing "_sa" part. This was intentionally left out because it isn't part of the function name)
OK. that worked too, without loading the toolbox, an shaved about 8 seconds off the process (34secs to 26 seconds for the script), so I'll going to switch the "correct answer" to the above although it is all very help ful and there is of course more than one way to handle it.
So, instead of my code
- myDir = arcpy.GetInstallInfo()["InstallDir"]
- toolboxPath = arcpy.os.path.join(myDir, "ArcToolbox\Toolboxes\Spatial Analyst Tools.tbx")
- arcpy.AddMessage("toolbox {}".format(toolboxPath))
- arcpy.AddToolbox(toolboxPath)
- #...
arcpy.ExtractValuesToPoints_sa(randomTemp, elevRaster, randomPtSet, "NONE", "VALUE_ONLY")
I'm switching to
import arcpy from arcpy.sa import * #... arcpy.sa.ExtractValuesToPoints(randomTemp, elevRaster, randomPtSet, "NONE", "VALUE_ONLY")
Both work, and the version/path check may come in handy for other things, but the second solution is what I will go with for now.
EDIT: fixed typo in second code block.
For the raster question, what happens if you call the function directly?
import arcpy from arcpy.sa import * arcpy.env.workspace = "C:/sapyexamples/data" arcpy.CheckOutExtension("Spatial") ExtractValuesToPoints("rec_sites.shp", "elevation", "C:/sapyexamples/output/outValPnts", "INTERPOLATE", "VALUE_ONLY")
I received an error when I try that.