Arcpy to get ArcGIS Application Data folder (My Toolboxes folder)
A handy hint for 'special' folders is os.environ.get
Some environments that will help you on windows:
AGSDESKTOPJAVA
ALLUSERSPROFILE
APPDATA
COMPUTERNAME
HOMEDRIVE
HOMEPATH
LOCALAPPDATA
NUMBER_OF_PROCESSORS
PROCESSOR_ARCHITECTURE
ProgramFiles
ProgramFiles(x86)
PUBLIC
SystemDrive
SystemRoot (don't write here, but it's handy for reading)
TEMP
TMP
USERNAME
USERPROFILE
windir
To see a list open a CMD window and type set
and press enter, a list will be printed of all environment variables and their values for your local computer.
Your path to toolboxes is aided by arcpy.GetInstallInfo():
"{0}\\ESRI\\Desktop{1}\\ArcToolbox\\My Toolboxes".format(os.environ.get("APPDATA"),arcpy.GetInstallInfo()["Version"])
As a one liner, broken up (for readability):
AppData = os.environ.get("APPDATA") # not case sensitive
II = arcpy.GetInstallInfo()
Version = II["Version"] # Case sensitive
TbxFld = "{0}\\ESRI\\Desktop{1}\\ArcToolbox\\My Toolboxes".format(AppData,Version)
As noted in the comments there is a bug in some versions with the GetInstallInfo()[Version]
relating to sub-versions, to avoid this one way would be to:
if len(arcpy.GetInstallInfo()["Version"].split('.')) > 2:
vString = '.'.join(arcpy.GetInstallInfo()["Version"].split('.')[:-1])
else:
vString = arcpy.GetInstallInfo()["Version"]
What I was looking for is:
arcpy.ImportToolbox(u'Toolboxes\\My Toolboxes\\Customtoolbox.tbx')
I keep my all toolboxes in this folder (Toolboxes\My Toolboxes).
When I need to access one of my custom GP tool from another toolbox I have to import that toolbox.