arcpy.AddToolbox - how to specify relative path for multiple ArcGIS versions

4732
13
Jump to solution
11-24-2015 04:01 PM
RebeccaStrauch__GISP
MVP Emeritus

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

0 Kudos
1 Solution

Accepted Solutions
FreddieGibson
Occasional Contributor III

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)

View solution in original post

13 Replies
RebeccaStrauch__GISP
MVP Emeritus

Excellent.  Thanks Freddie.  And I can see I'll be able to pull the path from that too.

0 Kudos
DanPatterson_Retired
MVP Emeritus

but get install version isn't backward compatible is it? obviously to the start of arcpy?

0 Kudos
FreddieGibson
Occasional Contributor III

92.png

RebeccaStrauch__GISP
MVP Emeritus

Good to know....for backwards compatibility.

0 Kudos
FreddieGibson
Occasional Contributor III

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)

RebeccaStrauch__GISP
MVP Emeritus

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

    1. myDir = arcpy.GetInstallInfo()["InstallDir"] 
    2. toolboxPath = arcpy.os.path.join(myDir, "ArcToolbox\Toolboxes\Spatial Analyst Tools.tbx") 
    3. arcpy.AddMessage("toolbox {}".format(toolboxPath)) 
    4. arcpy.AddToolbox(toolboxPath) 
    5. #...
    6. 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.

0 Kudos
FreddieGibson
Occasional Contributor III

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")
0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

I received an error when I try that.

0 Kudos