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

4716
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
13 Replies
DanPatterson_Retired
MVP Emeritus

Rebecca, sorry to add to the confusion rather than give an answer... but here goes.

Do you store your scripts and toolboxes in an esri/arcmap folder? even for conventional toolboxes (not python toolboxes or add-ins)?  The reason I ask, is because I gave up on calling "arcpy.LoadThisToolbox" stuff explicitly because of such issues and I also stopped from X import Y at about the same time.  I found that if I could just import arcpy and then refer arcpy.SomeTool_sa or arcpy.SomeTool_management without problem.  I have noticed a slightly different approach sneaking into the posts by esri staff lately which produces the same result but may be a suggestion for the future path to take  ie  ... arcpy.management.SomeTool( blah blah) .  So to date, I put my toolboxes and their associated scripts in a folder and load the toolbox from there.  This has worked for me with tools that I use since 9.3 to 10.3.1 (only tested a few in Pro 1.1.1).

RebeccaStrauch__GISP
MVP Emeritus

Thanks Dan.  I like the thought of accessing the few tools I need in a "lighter" manner, and that might have been what Freddie was aiming for too.  I'll have to give your suggestion a little more thought, but for now, I'm just trying to have something so I can move on. 

This is part of the addin I'm creating from my AML/Avenue days, with some added features.  I've been the only one using it in the past, but trying to make it more flexible so I can train others.  Chances are the user will have the latest version, but you never know.   I typically store my custom scripts and custom toolbar within the addin build structure.  I could copy the Tools I need directly into the custom toolbar I guess, and grab it from that maybe?  I'm not sure if that is what you are suggesting or not.  But in any case, it needs to be available to the addin.

As for you other comment about it being whether  arcpy.GetInstallInfo()["Version"] is backwards compatible...I'll have to test that.  But I think it will work for what I need for now (10.3)...and it looks like it's been available since at least 10.1.  I'm sure there would be other missing tools or issues if used with a version prioir to that....and actually I never liked the random points from a few version back....created random, then sorted them from north-to-south.....defeated the purpose for us. But that has been fixed now. 

FYI - this is the final code I added to grab the toolbox for this 10.3 installation anyway.

myDir = arcpy.GetInstallInfo()["InstallDir"]
toolboxPath = arcpy.os.path.join(myDir, "ArcToolbox\Toolboxes\Spatial Analyst Tools.tbx")
arcpy.AddMessage("toolbox {}".format(toolboxPath))
arcpy.AddToolbox(toolboxPath)

In case it helps anyone else.  Not the only way to do it, but it works for me.

FreddieGibson
Occasional Contributor III

When I need to write scripts that are backwards compatible with 9.X I always start by writing the script using the arcgisscripting to create the dispatch object and quickly check the install version. I will limit my use of arcpy in the code so that I can reduce the amount of redundancy. If there is a functionality that I need to use that is only available in 10.X and the users machine shows a valid version I'll quickly import arcpy and execute that task.

The biggest problem you'll see when writing a script that is compatible with 9.X is going to be the change in case for functions\methods. 9.X functions used PascalCase, whereas 10.X functions use camelCase. For example, if you're using a cursor at 9.X you'd need to call row.GetValue, whereas arcpy would require that you call row.getValue.

DanPatterson_Retired
MVP Emeritus

Thanks Freddie.  Yes the pascal vs camel case caused a load of issues but was easy to fix.  It is nice to know that getinstallversion simply returns a string so I can back-to-the-future some of my older toolsets in the future and branch them off to newer versions of code